I’ve been working on topcoder recently and I stumbled upon this question which I can’t quite make understand.
The question is to find F(n) = f(1)+f(2)+….+f(n) for a given “n” such that f(n) is the largest odd divisor for n.
There are many trivial solutions for the answer; however, I found this solution very intriguing.
int compute(n) {
if(n==0) return 0;
long k = (n+1)/2;
return k*k + compute(n/2);
}
However, I don’t quite understand how to obtain a recursive relation from a problem statement such as this. Could someone help out?
I believe they are trying to use the following facts:
Now split {1,2,…, 2m+1} as {1,3,5,7,…} and {2,4,6,…,2m} and try to apply the above facts.