Thread: Using for-loop and arrays to calculate factorials and fibonacci sequence

1. Member Join Date
Jan 2011
Posts
24
Rep Power
0 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:
Java 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];  Reply With Quote

2. 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.  Reply With Quote

3. Java 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.  Reply With Quote

4. Member Join Date
Jan 2011
Posts
24
Rep Power
0 Thank you Junky! (not factorial but an actual exclamation point))

After reading your righteous advice, I simply initialized arr for both the factorials and fibonacci sequence, the code now works as intended, and here it is:
Java Code:
public static long fib(long[] arr, int max) {

for (int i = 2; i <= max ; i++) {
arr=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;  //<---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];  Reply With Quote

5. Don't do it inside the loop.  Reply With Quote

6. Member Join Date
Jan 2011
Posts
24
Rep Power
0 well the code works fine now, what can happen if it is inside the loop?  Reply With Quote

7. 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:
Java Code:
int x;
x = 10;
x = 10;
x = 10;
x = 10;
x = 10;
x = 10;
etc
Pointless huh?  Reply With Quote

8. Member Join Date
Jan 2011
Posts
24
Rep Power
0 Alright I gotcha, thanks again.  Reply With Quote

9. Member Join Date
Mar 2011
Posts
1
Rep Power
0 you could do the fib sequence in a for loop without using arrays like this:
Java 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);
Last edited by SDC; 03-10-2011 at 03:52 AM.  Reply With Quote Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•