Given this pseudo code of a function
f(0) = 1;
f(1) = 3;
f(n) = 3 * f(n - 1) - f(n - 2); // for n >= 2.
Is there a non recursive way of doing this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, all recursive algorithms can be converted into iterative ones. The recursive solution to your problem is something like (pseudo-code):
Since you only have to remember the previous two terms to calculate the current one, you can use something like the following pseudo-code:
This simply handles the “recursive” termination condition first then iterates where it would normally call itself. At each iteration, you calculate the current term, then rotate the terms through the grandparent and parent.
There is no need to keep the grandparent around once you’ve calculated the current iteration since it’s no longer used.
In fact, it could be said that the iterative solution is better (from a performance viewpoint) since terms are not recalculated as they are in the recursive solution. The recursive solution does have a certain elegance about it though (recursive solutions generally do).
Of course, like the Fibonacci sequence, that value you calculate rises very quickly so, if you want what’s possibly the fastest solution (you should check all performance claims, including mine), a pre-calculated lookup table may be the way to go.
Using the following Java code to create a table of long values (that
whilecondition is just a sneaky trick to catch overflow, which is the point at which you can stop building the array):gives you an array definition that you can just plug in to a lookup function, as per the following example:
Interestingly enough, WolframAlpha comes up with a formulaic approach that doesn’t even use iteration. If you go to their site and enter
f(0)=1, f(1)=3, f(n)=3f(n-1)-f(n-2), you’ll get back the formula:Unfortunately, it may not be as fast as the iteration, given the limited number of input values that result in something that can fit in a Java
long, since it uses floating point. It’s almost certainly (but, again, you would need to check this) slower than a table lookup.And, it’s probably perfect in the world of maths where real-world limits like non-infinite storage don’t come into play but, possibly due to the limits of IEEE precision, it breaks down at higher values of
n.The following functions are the equivalent of that expression and the lookup solution:
Now we need a mainline to compare them:
This will output:
Looking good up to here, some more:
But then something starts going awry:
The fact that the above are tantalisingly close, and that the number of digits in the error is proportional to the number of digits in the result, indicates it’s probably a loss-of-precision problem.
After this point, the formulaic function just starts returning the maximum long value:
And then our lookup function breaks down as well since the numbers are too big for a long: