Now here's a more efficient algorithm which does produce the currect result:
int square(int num) { int a = 0; for (int x = 1, n = num; x <= num; x+=x, n+=n) { if( num & x) { a += n; } } return a; }
Now here's a more efficient algorithm which does produce the currect result:
It would be nice if this site allowed us to run/step through the code, to see exactly what it is doing.