Hmmm....
csharp
// generate an array of the first Nth Fibonacci numbers
int[] fibs(int num)
{
// fib has 3 base cases, and 3 starting numbers.
// Shouldnt be too hard.
// Lets handle the first three...
int[] fibs = new int[num];
for(int fibn = 0; fibn < num; fibn++)
{
// Lets make sure fibn isnt a special case:
switch(fibn)
{
// first Fibonacci is 0.
case 0:
fibs[fibn] = 0;
break;
// 1 and 2 are always 1;
case 1:
case 2:
fibs[fibn] == 1;
break;
default:
fibs[fibn] = fibs[fibn-1]+fibs[fibn-2];
break;
}
return fibs[];
}
}
.. Do what you wish with the results of fib(int);
Another cheap hack is to get the Nth fib:
csharp
function nthFib(int n)
{
// registers we'll use. Forgive me for thinking like a
int eaa,eab,eac;
// funky cases.
if(n == 0) {return 0; }
elseif(n == 1 && n ==2) {return 1; }
// the normal case.
else
{
// set our initial values.
eaa=1;eab=2;eac=0;
// go through the loop. We're assuming the 3rd fib was asked for (or where n > 2)
for(int x = 3; x<=n; x++)
{
// set our output register to the sum of the past 2
eac = eab+eaa;
// eab is the Last Computed value, so we push that into eaa
eaa=eab;
// and since we want to hold onto our last computed value, we put it into eab.
eab = eac;
}
// we've calculated enough iterations. RETURN TO BASE, Y'ALL!
return eac;
}
}
... dont even ask me to indent that.