\$\begingroup\$
\$\endgroup\$
2
The task
Implement division of two positive integers without using the division, multiplication, or modulus operators. Return the quotient as an integer, ignoring the remainder.
My solution
const division = (dividend, divisor) => {
let remainder = null;
let quotient = 1;
const sign = ((dividend > 0 && divisor < 0) ||
(dividend < 0 && divisor > 0)) ?
~1 : 1;
let tempdividend = Math.abs(dividend);
let tempdivisor = Math.abs(divisor);
if (tempdivisor === tempdividend) {
remainder = 0;
return sign;
} else if (tempdividend < tempdivisor) {
remainder = dividend < 0 ?
sign < 0 ? ~tempdividend : tempdividend :
tempdividend;
return 0;
}
while (tempdivisor << 1 <= tempdividend) {
tempdivisor = tempdivisor << 1;
quotient = quotient << 1;
}
quotient = dividend < 0 ?
(sign < 0 ? ~quotient : quotient) + division(~(tempdividend-tempdivisor), divisor) :
(sign < 0 ? ~quotient : quotient) + division(tempdividend-tempdivisor, divisor);
return quotient;
}
thadeuszlaythadeuszlay
asked Apr 18, 2019 at 16:57
1 Answer 1
\$\begingroup\$
\$\endgroup\$
6
Is bitwise operation mandatory? There's a much simpler way
const divide = (dividend, divisor) => {
let quotient = 0, neg = false;
if( (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0) ){ neg = true; }
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
if(dividend < divisor) {return 0;}
else if(dividend > 0 && divisor != 0){
while(dividend >= divisor){
dividend -= divisor;
++quotient;
}
} else { // handle what you want to do for those cases..}
return neg ? -quotient : quotient;
}
You get your quotient and remainder is ignored. Just do a check if dividend is negative or divisor is 0.
-
1\$\begingroup\$ Welcome to Code Review! Better check dividend sign before comparing to divisor. \$\endgroup\$greybeard– greybeard2019年04月18日 20:01:29 +00:00Commented Apr 18, 2019 at 20:01
-
1\$\begingroup\$ Also first if statement should be
<
not>
and ... It may be the simple way, but divide(2**32-1, 1) may take some time to complete (On average device 11 seconds), even longer if itsdivide(Number.MAX_SAFE_INTEGER, -1)
(about 22 years on average device) There is a point where complexity provides practical solutions. \$\endgroup\$Blindman67– Blindman672019年04月18日 23:49:22 +00:00Commented Apr 18, 2019 at 23:49 -
\$\begingroup\$ @greybeard I have fixed my answer to accommodate the negative cases. Thanks for the suggestion. \$\endgroup\$Dblaze47– Dblaze472019年04月19日 04:15:52 +00:00Commented Apr 19, 2019 at 4:15
-
\$\begingroup\$ @Blindman67 I have fixed the comparison. As for the division case, your case is a valid example that I have taken into account before posting the answer. However, there is no one solution to solve them all, and I thought if the OP was handling smaller cases (possible range was not mentioned), and has already a solution to handle complex cases, why not a simpler one then. \$\endgroup\$Dblaze47– Dblaze472019年04月19日 04:23:09 +00:00Commented Apr 19, 2019 at 4:23
-
\$\begingroup\$ @Dblaze47 Oh my bad, my numbers were way wrong 32Bit int is ~7 seconds, and MAX_SAFE_INTEGER is about ~5 months. Anyways besides the point, "Why not a simpler one then." Why? This is code review, the focus is the review of OP's code, its style, its logic, and with that you can add example alternative/s demonstrating the points reviewed. The OP is after all looking for feedback on their code. If alternatives were the quest then google would be the better path, don't you think. \$\endgroup\$Blindman67– Blindman672019年04月19日 15:50:18 +00:00Commented Apr 19, 2019 at 15:50
Explore related questions
See similar questions with these tags.
lang-js
division(-1,1)
returns-2,
-2 / -1` returns3
and worst any value divide 0 does not return at all. \$\endgroup\$