Question d’entretien chez Google

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

Réponses aux questions d'entretien

Utilisateur anonyme

15 juil. 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

Utilisateur anonyme

6 mai 2009

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

5

Utilisateur anonyme

6 mai 2009

You can do division with shifts and subtracts...

Utilisateur anonyme

8 mai 2009

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

1

Utilisateur anonyme

14 janv. 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.

Utilisateur anonyme

6 févr. 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....

Utilisateur anonyme

5 mai 2009

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

Utilisateur anonyme

5 mai 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?