Hi have to calculate the n term of a series, being n really big, and it has to be done as fast as posible.
The series is defined by the following function:
f(0) = 1
f(1) = 1
f(2n) = f(n)
f(2n+1) = f(n) + f(n-1)
I know I have to use memoization. I did this code but the problem is that with big n values is giving segmentation fault. I would like to try to do a 2 value array version (like the one described in one of the responses here), but I'm not reaching the proper solution.
uint64_t f(uint64_t n)
{
uint64_t a[n+2];
uint64_t i;
a[0] = 1;
a[1] = 1;
for(i = 2; i <= n; i++)
{
if (i % 2 == 0)
{
a[i] = a[i / 2];
}
else
{
a[i] = a[i / 2] + a[(i / 2) - 1];
}
}
return a[n];
};