Question d’entretien chez Meta

Implement division without using multiplication or division. It should work most efficient and fast.

Réponses aux questions d'entretien

Utilisateur anonyme

20 sept. 2011

exp(ln(a)-ln(b))=a/b

11

Utilisateur anonyme

22 sept. 2011

What if one or both of a,b is less than zero. ln(x) for x < 0 is not defined.

3

Utilisateur anonyme

17 mai 2012

Obviously the interviewer would not allow us to use Math functions like exp, log etc. We are supposed to use the Long division method or the Newton Raphson method to find the quotient. Newton Raphson is the fastest but uses operator * (multiplication) though.

2

Utilisateur anonyme

19 janv. 2013

Python version that gives you an idea how it works: i = 0 while divisor = 0: if dividend >= divisor: dividend -= divisor result |= 1 >= 1 i-=1 plus some code to check for 0 and support negative values

1

Utilisateur anonyme

6 juin 2014

#Write a program to do division without division or multiplication 2 3 def division(dividend, divisor_initial): 4 divisor_final = divisor_initial 5 quotient = 1 6 while dividend - divisor_final > divisor_initial: 7 quotient += 1 8 divisor_final = divisor_final + divisor_initial 9 number = divisor_final - divisor_initial 10 remainder = dividend - divisor_final 11 return quotient,remainder 12 13 14 def main(): 15 print division( 101, 3) 16 17 18 if __name__ == "__main__": 19 main()

Utilisateur anonyme

13 nov. 2012

http://stackoverflow.com/a/5284915

Utilisateur anonyme

8 oct. 2011

This solution rounds down to the nearest signed integer // Implement division without using multiplication or division. It should work most efficient and fast. int Divide(int divisor, int dividend) { int divisionCount; int tmp = dividend; if (tmp - divisor > 0) { tmp = tmp - divisor; divisionCount++; } // This will apply the correct sign to the quotient if ((divisor & 8) ^ (dividend & 8) != 0) { divisionCount = divisionCount | 8; } return divisionCount; }

Utilisateur anonyme

8 oct. 2011

// correcting previous answer int Divide(int divisor, int dividend) { int divisionCount; int tmp = dividend; if (tmp - divisor > 0) { tmp = tmp - divisor; divisionCount++; } // This will apply the correct sign to the quotient if ((divisor & 0x80000000) ^ (dividend & 0x80000000) != 0) { divisionCount = divisionCount | 80000000; } return divisionCount; }

Utilisateur anonyme

28 oct. 2011

public class Solution { public static void main(String[] args){ int top=32; int bottom=4; int count=0; boolean negative=(top*bottom)=bottom){ top=top-bottom; count++; } System.out.print((negative)?"-":""+String.valueOf(count)+"..."+top); } }

Utilisateur anonyme

13 oct. 2011

can anyone post solution in java?

Utilisateur anonyme

28 sept. 2011

we can use bit shift operator. e.g. 4 is 100 in binary we want to divide 4 by 2 so right shift 4 by 1 bit 4>>1, so we get 010 which is 2.

1