Pergunta de entrevista da empresa Amazon

Divide without the / operator.

Respostas da entrevista

Sigiloso

21 de jan. de 2014

bitshift

1

Sigiloso

27 de jan. de 2014

An efficient solutions is to use binary search. One could implement it like this {code} public int divide(int nom, final int den) { if (den == 0) { throw new IllegalStateException("Divide by 0"); } //make nom be dividable by den nom -= nom % den; if (nom >> 1; long curr = (long) mid * (long) den; while (curr != nom) { if (curr > nom) { r = mid - 1; } else { l = mid + 1; } mid = (l + r) >>> 1; curr = (long) mid * (long) den; } return mid; } {code}

Sigiloso

31 de jan. de 2014

#!/usr/bin/python -tt def div(a, b): """ To divide a by b without / operator, assuming a >= 0 and b > 0. Result floored to the nearest integer. """ c = 0 d = b while True: if d > a: break else: c = c + 1 d = d + b return c