Divide two integers without using multiplication, division and mod operator


In this problem, we simply need to divide two integers without using multiplication, division and mod operator. Though we can use addition or multiplication or bit manipulation.

The problem statement states that we will be given two integers x and y. Without using multiplication, division or mod operator, we need to determine the quotient after dividing x by y.

Example

INPUT: x=15 , y=5

OUTPUT: 3

INPUT: x=10 , y=4

OUTPUT: 2

INPUT: x=-20 , y=3

OUTPUT: -6

Approach

Approach-1(using simple mathematics)

In this approach, we will use a simple mathematics algorithm. Below is the step-by-step illustration of the steps we are going to follow −

  • We will keep deducting the divisor i.e. y from the dividend i.e. x until x is greater than or equal to y.

  • When y becomes greater than x i.e divisor is greater than dividend, the dividend becomes the remainder and the number of times the subtraction is performed becomes the quotient.

  • Store the number of times the subtraction performed in a variable and return it which is our desired output.

Example

Below is the C++ implementation of the above algorithm &minnus;

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long division(long long a,long long b) // where a is dividend and b is divisor
{
   long long sign=1;
   if((a<0) ^( b<0))  // - ^ - = +,+ ^ - = - , - ^ + = - , + ^ + = +
   {
      sign=-1; 
   }
   long long m=abs(a);
   long long n=abs(b);
   long long count=0; // for storing the quotient 
   while(m>=n){
      m=m-n;
      count++;
   }
   if(sign==-1) // when sign is negative
   {
      count=-count;
   }
   return count;
} 
int main(){
   long long a=-21474;
   long long b=2;
   long long val=division(a,b);
   cout<<val<<endl;
   return 0;
}

Output

-10737

Time Complexity: O(a/b)

Space Complexity: O(1)

Approach-2 (using bit manipulation)

  • As any number can be represented in the form of 0 or 1, the shift operator can be used to represent the quotient in binary form.

  • Iterate on the bit position from 31 to 1 of the divisor using a for loop.

  • Find the first bit where divisor i.e b<<i is less than the dividend holds true, then keep updating ith bit position where it holds true in a variable temp.

  • On verifying the next position, add the result to the temp variable to ensure that temp+(b<<i) is less than or equal to dividend i.e. a.

  • Update the quotient everytime by calculating quotient OR 1<<i.

  • Return the quotient after updating the corresponding sign.

Example

Below is the C++ implementation of the above approach −

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long division(long long a,long long b) // where a is dividend and b is divisor
{
   long long sign=1;
   if((a<0) ^( b<0))  // - ^ - = +,+ ^ - = - , - ^ + = - , + ^ + = +
   {
      sign=-1; 
   }
   long long m=abs(a);
   long long n=abs(b);
   long long count=0; // for storing the quotient 
   long long temp=0;
   for (int j = 31; j >= 0; --j){
   
      if (temp + (n << j) <= m){
         temp += n << j;
         count |= 1L << j;
      }
   }
   if(sign==-1) // when sign is negative
   {
      count=-count;
   }
   return count;
   
} 
int main(){
   long long a=49;
   long long b=5;
   long long val=division(a,b);
   cout<<val<<endl;
   a=-18,b=5;
   cout<<division(a,b);
   
   return 0;
}

Output

9
-3

Time Complexity : O(log(a))

Space Complexity: O(1), since it uses no extra space.

Approach-3 (using the logarithmic function)

In this approach, we will use a simple logarithm function to calculate the quotient.

As we all know,

$$\mathrm{In(\frac{a}{b})\:=\:In(a)\:-\:In(b)}$$

which further can be modified into

$$\mathrm{\frac{a}{b}\:=\:e^{(In(a)\:-\:In(b))}}$$

So this is the basic idea to solve the given problem using this efficient approach.

Below are the step-by-step illustration of the approach that we are going to follow −

  • If either of them i.e. dividend or divisor is 0, we will return 0.

  • Now, we will check for signs using an exclusive OR function (XOR) to store signs in a variable.

  • If the divisor is 1, then simply return the dividend.

  • Now, declare a variable and store the value equal to $\mathrm{e^{(In(a)\:-\:In(b))}}$ in it using exp function and log function.

  • Log and exp is a built-in function in C++. Log function returns the natural log value of the entered number and exp returns the value equal to e raised to the entered value.

Example

Below is the C++ implementation of the above approach −

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long int divide(long long int a,long long int b){
   long long int sign=1;
   if(a==0||b==0) // when a is zero or b is zero
   {
      return 0;
   }
   if((a>0) ^ (b>0)) // - ^ - = +,+ ^ - = - , - ^ + = - , + ^ + = +
   {
      sign=-1;
   }
   if(b==1) // when b is 1 then it will return a example 51/1 = 51
   {
      sign==-1?-a:a;
      return a;
   }
   long long int m=abs(a);
   long long int n=abs(b);
   
   //log function return the logarithmic value of the entered value with base e i.e. natural log of the entered value
   //exp function return the value equal to e^(entered value)
   long long int ans =exp(log(m) - log(n)) + 0.0000000001; 
   
   // if it gives the value in decimal we will add from 0.0000000001 to account for accuracy errors
   if(sign==-1) // when sign is negative return the negative ans
   {
      return -ans;
   }
   return ans;
   
}
int main(){
   long long int ans=divide(47,-9);
   cout<<ans<<endl;
   
   return 0;
}

Output

-5

Time Complexity : O(1), since constant time is taken to perform the operation.

Space Complexity : O(1), since it uses no extra space.

Conclusion

In this article, we learn to divide two integers without using multiplication, division or mod operator. We have learned to solve the problem with different approaches with different efficiencies. They are using simple mathematics, using bit manipulation and using logarithmic functions. Among all of them, using a log function is the most efficient approach as it has O(1) time complexity which is the least among all the approaches.

I hope this article helps you to solve all your concepts regarding the topic.

Updated on: 14-Mar-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements