Getting a term in the fibonacci sequence without recursion - Printable Version +- BP Forums (https://bpforums.info) +-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55) +--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56) +---- Forum: Java (https://bpforums.info/forumdisplay.php?fid=19) +---- Thread: Getting a term in the fibonacci sequence without recursion (/showthread.php?tid=592) |
Getting a term in the fibonacci sequence without recursion - brandonio21 - 08-06-2012 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] |