Sollicitatievraag bij IBM

Optimize this code (like what compiler would reconstruct an equivalent source code as): int fn (int a, int b) { int sum = 0; for(int i=4*a;i>0;i--) { sum+=b*i*i; } return sum; }

Antwoorden op sollicitatievragen

Anoniem

5 sep 2013

One of them is replace 4*b with a<<2. But there are more ways I didn't find.

Anoniem

12 dec 2013

the loop can be replaced by one line (basic series formula) : sum = b* 4*a *(4*a +1) *(8*a+1)/6 http://www.tug.org/texshowcase/cheat.pdf