# Using for-loop and arrays to calculate factorials and fibonacci sequence

• 02-22-2011, 10:01 PM
baumboards
Using for-loop and arrays to calculate factorials and fibonacci sequence
The code is to calculate the fibonacci sequence and factorials, using two types of parameters. My code works for the first parameters of the fibonacci sequence and factorial, but the array part of the fibonacci code and factorial code doesn't work. I know I am very close, here it is:
Code:

```public static long fib(int max) {               long subTotal = 0;         int a=0,b=1,f=0;         for (int i = 2; i <= max; i++) {                         f=a+b;             a=b;             b=f;                         System.out.println("Fibonacci " + i + " value " + f);         }         return f;     }             public static long fib(long[] arr, int max) {                       for (int i = 2; i <= max ; i++) {             if (arr[i] == 0) {                  //this is the part that I can't figure out                 arr[i] = arr[i-1] + arr[i-2];                    System.out.println("Fibonacci[" + i + "] value " + arr[i]);             }         }         return arr[max];     }                     public static long factorial(int max) {                 long subTotal = 1;         for (int i = 2; i <= max; i++) {             subTotal = subTotal*i;                         System.out.println("factorial " + i + " value " + subTotal);         }         return subTotal;     }             public static long factorial(long[] arr, int max) {                       for (int i = 1; i <= max ; i++) {             if (arr[i] == 0)                                     //this is the second part that I can't figure out                     arr[i]= i*arr[i-1];                  System.out.println("factorial[" + i + "] value " + arr[i]);             }                 return arr[max];```
• 02-23-2011, 01:04 AM
sunde887
What is the input and output supposed to look like, that may help you out a bit, and it will help us help you as well.
• 02-23-2011, 01:12 AM
Junky
Code:

```public static long fib(long[] arr, int max) {                       for (int i = 2; i <= max ; i++) {             if (arr[i] == 0) {                  //this is the part that I can't figure out                 arr[i] = arr[i-1] + arr[i-2];                    System.out.println("Fibonacci[" + i + "] value " + arr[i]);             }         }         return arr[max];     }```
We know that in the fibonacci sequence the first 2 numbers are 1. So why not insert them into the array before doing anything else. Why bother with the if statement? To calculate every number after the first 2 we add the previous 2 numbers. No if's buts or maybes. Just do it.
• 02-23-2011, 06:03 AM
baumboards
Thank you Junky! (not factorial but an actual exclamation point))
After reading your righteous advice, I simply initialized arr[1] for both the factorials and fibonacci sequence, the code now works as intended, and here it is:
Code:

``` public static long fib(long[] arr, int max) {               for (int i = 2; i <= max ; i++) {             arr[1]=1;                          //<----this is the part that did the trick             arr[i] = arr[i-1] + arr[i-2];                                System.out.println("Fibonacci[" + i + "] value " + arr[i]);                     }         return arr[max]; public static long factorial(long[] arr, int max) {               for (int i = 2; i <= max ; i++) {           arr[1]=1;  //<---the same initialization as above fixed the code here           arr[i]= i*arr[i-1];                              System.out.println("factorial[" + i + "] value " + arr[i]);             }                 return arr[max];```
• 02-23-2011, 06:17 AM
Junky
Don't do it inside the loop.
• 02-23-2011, 08:36 PM
baumboards
well the code works fine now, what can happen if it is inside the loop?
• 02-23-2011, 11:47 PM
Junky
Nothing bad can happen. It was just a micro optimisation. You want to assign the value once not everytime around a loop. your code is equivalent of:
Code:

```int x; x = 10; x = 10; x = 10; x = 10; x = 10; x = 10; etc```
Pointless huh?
• 02-24-2011, 05:34 AM
baumboards
Alright I gotcha, thanks again.
• 03-10-2011, 04:16 AM
SDC
you could do the fib sequence in a for loop without using arrays like this:
Code:

``` int i, j=1,k=0,max=20; for (i=1;i<=max;i++,k+=j,j=k-j) System.out.println("Fibonacci[" + i + "] value " + k);```