3
\$\begingroup\$

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;
 }
\$\endgroup\$
  • 1
    \$\begingroup\$ Your function does not work, very sloppy. I am amazed you found two arguments that would return the correct result. division(-1,1) returns -2, -2 / -1` returns 3 and worst any value divide 0 does not return at all. \$\endgroup\$ – Blindman67 Apr 18 at 23:50
  • 1
    \$\begingroup\$ It works for positive integers (as the task ask for). But you are right about the other stuff. Will fix it. @Blindman67 \$\endgroup\$ – thadeuszlay Apr 19 at 8:56
3
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to Code Review! Better check dividend sign before comparing to divisor. \$\endgroup\$ – greybeard Apr 18 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 its divide(Number.MAX_SAFE_INTEGER, -1) (about 22 years on average device) There is a point where complexity provides practical solutions. \$\endgroup\$ – Blindman67 Apr 18 at 23:49
  • \$\begingroup\$ @greybeard I have fixed my answer to accommodate the negative cases. Thanks for the suggestion. \$\endgroup\$ – Dblaze47 Apr 19 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 Apr 19 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 Apr 19 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.