Results 1 to 6 of 6
- 03-01-2011, 07:11 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
Fibonacci sequence using iterative and recursive method
I am to produce the fibonacci sequence using one parameter, then two parameters. I am also supposed to do it with an iterative and recursive method, I know im close by using the general formula for fibonacci: f(x)=f(x-1)+f(x-2)
But im having trouble with the base case:
Java Code:public class Lab21_7 { public static void init(long[] arr) { // Initialize all the entries of the array to 0 for (int i = 0; i < arr.length; i++) { arr[i] = 0; } } public static long fib(int max) { // sumAll basic version System.out.println("Called Fibonacci(" + max + ")"); long result; if (max <= 1) //<----this part messes me up result = 1; else result = fib(max-2)+fib(max-1); System.out.println("Return Fibonacci(" + max + ") = " + result); return result; public static long fib(long[] arr, int max) { // Simple Recursion with array System.out.println("Called Fibonacci(" + max + ")"); if (max <= 1) arr[max] = 1; else if (arr[max] == 0) arr[max] = fib(max-2) + fib( arr,max - 1); System.out.println("Return Fibonacci(" + max + ") = " + arr[max]); return arr[max]; }
- 03-01-2011, 07:38 PM #2
What does your code do instead? You might consider posting an SSCCE that we can run.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-01-2011, 07:47 PM #3
Take a look here for help with fibonacci numbers.
- 03-02-2011, 05:25 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
My current code prints out the incorrect fibonacci sequence, I implemented the fibonacci code from the wikipedia website and it returned the correct sequence, except there were gaps in the sequence. i.e. 1,1,2,3,5,0,0,8.....
- 03-02-2011, 01:32 PM #5
Like I said, you might want to post an SSCCE that shows us what you're actually doing. We can't see how you're calling these methods to produce that output, so we can't really guess at what your problem is.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
-
Hijack post deleted. Poster warned not to hijack in another's thread to start their own thread.
Similar Threads
-
Using for-loop and arrays to calculate factorials and fibonacci sequence
By baumboards in forum New To JavaReplies: 8Last Post: 03-10-2011, 03:16 AM -
Help needed to turn a recursive method into iterative
By mercyfulfate in forum New To JavaReplies: 5Last Post: 07-16-2010, 06:13 PM -
Turning Recursion Method into Iterative method
By mattakuevan in forum New To JavaReplies: 9Last Post: 06-15-2010, 06:46 AM -
Fibonacci sequence
By ŖàΫ ỏƒ Ңόρę in forum New To JavaReplies: 6Last Post: 03-25-2010, 06:59 AM -
Java class HashIt with a static recursive method and a static iterative method
By kezkez in forum New To JavaReplies: 3Last Post: 02-09-2010, 05:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks