Originally Posted by

**helloworld922**
In a loop, still O(n) time. True dat it's only a loop of 5, but it's still nice to be efficient

Sorry, I meant to calculate the series over and over again in a loop. For example (using your fib example function)

int n = 5;
for ( int i = 0; i < n; i++ ){
int fib = fib(i);
}

As the loop goes through, it recalculates the previously calculated fibonacci values, doing so in a drawing routine which may be called often. Of course, for 5 its not a big deal, but larger values the computation time starts to pile up

This is as opposed to previously computing the series up to n, then accessing these as needed

/**
*Returns an array containing the Fibonacci sequence up to n in the series
*/
public static int[] fib(int n)
{
int fib[] = new int[n];
if (n < 3)
{
fib[0] = 1;
if ( n == 2 )
fib[1] = 1;
}
for (int i = 3; i < n; i++)
{
fib[i] = fib[i-1] + fib[i-2];
}
return fib;
}
int fib[] = fib(n);//even better to do this upon construction or whenever the value of n is set
for ( int i = 0; i < n; i++ ){
int num = fib[i];
///do the calculations and drawing
}