Google interview question

How do you code integer division without using divider ('/')

Interview Answers

Anonymous

15 Jul 2009

You iteratively perform subtractions, which is all that division is. If there is a remainder, you multiply it by 10 and repeat your iterative subtractions. The number of times you need to subtract is represented in a counter, which becomes the digits within the result.

12

Anonymous

6 May 2009

exp(log(a)-log(b))

5

Anonymous

6 May 2009

You can do division with shifts and subtracts...

Anonymous

8 May 2009

Multiply by the inverse of the number you wish to divide by.

1

Anonymous

14 Jan 2010

Well, you can do what we did in Grade 5 or so, memorize a multiplication table. That will help us doing long division. We can create some sort of multiplication table in some hash. So while we do the long division, we do instant lookup within the hash. And we subtract.

Anonymous

6 Feb 2010

int a = 20; int diviser = 2; int x; for (x = 0; a!= 0; x++) a=a-diviser; you will have 10 iterations, giving you your answer....

Anonymous

5 May 2009

Code using a multiplier ('*') and the decimal equivalent of the fraction you're dividing by?

Anonymous

5 May 2009

But if you use the decimal equivalent you would need to use the divider (for example, 10/4, the decimal equivalent of 1/4 is .25, but how could you get that without dividing 1 by 4?). The only solution I can think of would be a guess and check that would work like a dictionary search. So for x/y=z (solve for z), if y is less than x we would see if y*10 is greater than x. If it is, 1 < z <10. We could then check to see if y*5 is greater than x. If so that 1 < z < 5. We could continue cutting in half until we get our desired approximate range, say 3 decimal places. This could be done recursively as well. Anyone have another idea?