Divide two integers without using multiplication, division and mod operator | Set2
Given two integers say a and b. Find the quotient after dividing a by b without using multiplication, division and mod operator.
Examples:
Input: a = 10, b = 3
Output: 3
Input: a = 43, b = -8
Output: -5
This problem has been already discussed here. In this post, a different approach is discussed.
Approach :
- Let a/b = c.
- Take log on both sides
- log(a) – log(b) = log(c)
- Now Log of RHS can be written as exp in LHS
- Final formula is : exp(log(a) – log(b)) = c
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void Divide( int a, int b)
{
long long dividend = ( long long )a;
long long divisor = ( long long )b;
long long sign = (dividend < 0) ^ (divisor < 0) ? -1 : 1;
dividend = abs (dividend);
divisor = abs (divisor);
if (divisor == 0) {
cout << "Cannot Divide by 0" << endl;
return ;
}
if (dividend == 0) {
cout << a << " / " << b << " is equal to : "
<< 0 << endl;
return ;
}
if (divisor == 1) {
cout << a << " / " << b << " is equal to : "
<< sign * dividend << endl;
return ;
}
cout << a << " / " << b << " is equal to : "
<< sign * exp ( log (dividend) - log (divisor))
<< endl;
}
int main()
{
int a = 10, b = 5;
Divide(a, b);
a = 49, b = -7;
Divide(a, b);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void Divide( int a, int b)
{
long dividend = ( long )a;
long divisor = ( long )b;
long sign = (dividend < 0 ) ^
(divisor < 0 ) ? - 1 : 1 ;
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
if (divisor == 0 )
{
System.out.println( "Cannot Divide by 0" );
return ;
}
if (dividend == 0 )
{
System.out.println(a + " / " + b +
" is equal to : " + 0 );
return ;
}
if (divisor == 1 )
{
System.out.println(a + " / " + b +
" is equal to : " +
sign * dividend);
return ;
}
System.out.println(a + " / " + b + " is equal to : " +
Math.floor(sign *
(Math.exp(Math.log(dividend) -
Math.log(divisor)))));
}
public static void main (String[] args)
{
int a = 10 , b = 5 ;
Divide(a, b);
a = 49 ; b = - 7 ;
Divide(a, b);
}
}
|
Python3
import math
def Divide(a, b):
dividend = a;
divisor = b;
sign = - 1 if ((dividend < 0 ) ^
(divisor < 0 )) else 1 ;
dividend = abs (dividend);
divisor = abs (divisor);
if (divisor = = 0 ):
print ( "Cannot Divide by 0" );
if (dividend = = 0 ):
print (a, "/" , b, "is equal to :" , 0 );
if (divisor = = 1 ):
print (a, "/" , b, "is equal to :" ,
(sign * dividend));
print (a, "/" , b, "is equal to :" ,
math.floor(sign * math.exp(math.log(dividend) -
math.log(divisor))));
a = 10 ;
b = 5 ;
Divide(a, b);
a = 49 ;
b = - 7 ;
Divide(a, b);
|
C#
using System;
class GFG
{
static void Divide( int a, int b)
{
long dividend = ( long )a;
long divisor = ( long )b;
long sign = (dividend < 0) ^
(divisor < 0) ? -1 : 1;
dividend = Math.Abs(dividend);
divisor = Math.Abs(divisor);
if (divisor == 0)
{
Console.WriteLine( "Cannot Divide by 0" );
return ;
}
if (dividend == 0)
{
Console.WriteLine(a + " / " + b +
" is equal to : " + 0);
return ;
}
if (divisor == 1)
{
Console.WriteLine(a + " / " + b +
" is equal to : " +
sign * dividend);
return ;
}
Console.WriteLine(a + " / " + b + " is equal to : " +
Math.Floor(sign *
(Math.Exp(Math.Log(dividend) -
Math.Log(divisor)))));
}
public static void Main ()
{
int a = 10, b = 5;
Divide(a, b);
a = 49; b = -7;
Divide(a, b);
}
}
|
PHP
<?php
function Divide( $a , $b )
{
$dividend = $a ;
$divisor = $b ;
$sign = ( $dividend < 0) ^
( $divisor < 0) ? -1 : 1;
$dividend = abs ( $dividend );
$divisor = abs ( $divisor );
if ( $divisor == 0)
{
echo "Cannot Divide by 0" ;
echo "" ;
}
if ( $dividend == 0)
{
echo $a , " / " , $b ,
" is equal to : " , 0 ;
echo "" ;
}
if ( $divisor == 1)
{
echo $a , " / " , $b ,
" is equal to : " ,
$sign * $dividend . "\n" ;
echo "" ;
}
echo $a , " / " , $b ,
" is equal to : " ,
$sign * exp (log( $dividend ) -
log( $divisor )). "\n" ;
echo "" ;
}
$a = 10;
$b = 5;
Divide( $a , $b );
$a = 49;
$b = -7;
Divide( $a , $b );
?>
|
Javascript
<script>
function Divide(a, b)
{
var dividend = a;
var divisor = b;
var sign = (dividend < 0) ^
(divisor < 0) ? -1 : 1;
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
if (divisor == 0)
{
document.write( "Cannot Divide by 0" );
return ;
}
if (dividend == 0)
{
document.write(a + " / " + b +
" is equal to : " + 0 + "<br>" );
return ;
}
if (divisor == 1)
{
System.out.println(a + " / " + b +
" is equal to : " +
sign * dividend + "<br>" );
return ;
}
document.write(a + " / " + b + " is equal to : " +
Math.floor(sign *
(Math.exp(Math.log(dividend) -
Math.log(divisor)))) + "<br>" );
}
var a = 10, b = 5;
Divide(a, b);
a = 49; b = -7;
Divide(a, b);
</script>
|
Output:
10 / 5 is equal to : 2
49 / -7 is equal to : -7
Time complexity: O(1)
Auxiliary space: O(1)
Last Updated :
19 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...