Results 1 to 9 of 9
 02222011, 09:01 PM #1Member
 Join Date
 Jan 2011
 Posts
 24
 Rep Power
 0
Using forloop 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[i1] + arr[i2]; 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[i1]; System.out.println("factorial[" + i + "] value " + arr[i]); } return arr[max];
 02232011, 12:04 AM #2
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
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.
 02232011, 12:12 AM #3Java 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[i1] + arr[i2]; System.out.println("Fibonacci[" + i + "] value " + arr[i]); } } return arr[max]; }
 02232011, 05:03 AM #4Member
 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[1] 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]=1; //<this is the part that did the trick arr[i] = arr[i1] + arr[i2]; 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[i1]; System.out.println("factorial[" + i + "] value " + arr[i]); } return arr[max];
 02232011, 05:17 AM #5
Don't do it inside the loop.
 02232011, 07:36 PM #6Member
 Join Date
 Jan 2011
 Posts
 24
 Rep Power
 0
well the code works fine now, what can happen if it is inside the loop?
 02232011, 10:47 PM #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
 02242011, 04:34 AM #8Member
 Join Date
 Jan 2011
 Posts
 24
 Rep Power
 0
Alright I gotcha, thanks again.
 03102011, 03:16 AM #9Member
 Join Date
 Mar 2011
 Posts
 1
 Rep Power
 0
Similar Threads

Fibbonaci sequence without arrays
By sunde887 in forum New To JavaReplies: 17Last Post: 01062011, 06:13 AM 
Fibonacci sequence
By ŖàΫ ỏƒ Ңόρę in forum New To JavaReplies: 6Last Post: 03252010, 06:59 AM 
loop problemarrays
By ester in forum New To JavaReplies: 0Last Post: 02022010, 09:44 PM 
Linked list sequence and array sequence
By Predz in forum New To JavaReplies: 1Last Post: 12312009, 01:30 AM 
Factorials help
By jhering1986 in forum New To JavaReplies: 3Last Post: 04062009, 01:50 AM
Bookmarks