08-06-2012, 11:51 PM
Here is a quick code snippet that gets the specified term of the Fibonacci sequence without recursion.
[code2=java]public long GrabFibTerm(int term)
{
if (term == 1) return 0;
if (term == 2) return 1;
long a = 0;
long b = 1;
int count = 2;
while (count <= term)
{
long oldB = b;
b = (a + b);
a = oldB;
count++;
}
return b;
}[/code2]
[code2=java]public long GrabFibTerm(int term)
{
if (term == 1) return 0;
if (term == 2) return 1;
long a = 0;
long b = 1;
int count = 2;
while (count <= term)
{
long oldB = b;
b = (a + b);
a = oldB;
count++;
}
return b;
}[/code2]