Implement integer division without using the division operator. That is, given a numerator (dividend) and denominator (divisor), return the quotient and remainder, or return an error when the denominator is zero. Either or both of the inputs may be negative. The remainder should have the same sign as the denominator. Of course, the absolute value of the remainder should be less than that of the denominator. Basically, use binary search to find the quotient. That is, use the numerator and denominator to bracket the possible range for the quotient, then use binary search within that range to determine the quotient that, when multiplied by the given denominator, brings you closest to the numerator without going over (or under, for negative denominators).
ajewolejude / quotientbinarysearch Goto Github PK
View Code? Open in Web Editor NEWImplement integer division without using the division operator. That is, given a numerator (dividend) and denominator (divisor), return the quotient and remainder, or return an error when the denominator is zero. Either or both of the inputs may be negative. The remainder should have the same sign as the denominator. Of course, the absolute value of the remainder should be less than that of the denominator. Basically, use binary search to find the quotient. That is, use the numerator and denominator to bracket the possible range for the quotient, then use binary search within that range to determine the quotient that, when multiplied by the given denominator, brings you closest to the numerator without going over (or under, for negative denominators).