An interview question.
How to implement division by addition? suppose they are all int.
My idea
- Add divisor to itself until it is larger than dividend. Each iteration, keep the sum result before addition.
- The quotient is the sum result before the last addition. the remainder can be counted by adding 1 until the
quotient * divisor + reminder == dividend
.
It is O(e^n)
, any better ideas? bit operation?