Domanda di colloquio di Amazon

Write an algorithm/code in C to do integer multiplication and division without using multiplication nor division operators. Do the same thing for partitioned algorithm/parallelizable (suitable in parallel processing)

Risposte di colloquio

Anonimo

8 mar 2012

The simplest way: int c = a*b c = 0; if ((a==0) || (b == 0) c = 0; if (a==1) c = b; else if (b==1) |c = a else for(i=0; i> 1; for(i=0; i>1); if (y % 2 == 0) return z<<1; else { return x + (z<<1); } } THere are other methods, though. Anybody can answer?

3

Anonimo

5 giu 2012

Stackoverflow has a good description of algorithm. Below is the python code. I am assuming 32 bit integers powersof2=[2**i for i in range(32)] def multiply(a,b): result=0 for j in range(32): if (powersof2[j] & b): print powersof2[j] & b result += (a << j) return result def divide(a,b): result = 0 for j in range(16,-1,-1): if (b << j) <= a: result += powersof2[j] a -= (b << j) print a return result

Anonimo

19 mar 2012

Not a C Guy, but if in Java, you can do it as a method that returns for mutiplcation return Math.pow(10, Math.log10(a)+Math.log10(b)) division: return Math.pow(10, Math.log10(a)-Math.log10(b)) where a and b are the numbers