943,761 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1846
  • Java RSS
Sep 14th, 2008
0

help needed with recursive fibonacci

Expand Post »
Im attemtping to return the next value in this suequence recursively 1, 4, 11, 34, 101
this is what i've done thus far, im not sure how to pass this list to the function to compute the next value which i believe to be 304. can some explain how i shoud write the main.
also in cases where the nth-1 value etc is needed how do you go about getting these list to the method

Thank you guys.




Java Syntax (Toggle Plain Text)
  1.  
  2. public static int fibo(int nthfibo)
  3. {
  4. if (nthfibo == 1)
  5. return 1;
  6. else if (nthfibo != 1 && nthfibo % 2 ==0)
  7. {
  8. return fibo(nthfibo) *3-1;
  9. }
  10. else
  11. {
  12. return fibo(nthfibo)*3+1;
  13. }
  14.  
  15. }
Reputation Points: 10
Solved Threads: 0
Light Poster
SyLk is offline Offline
27 posts
since Oct 2004
Sep 14th, 2008
0

Re: help needed with recursive fibonacci

One, multiplying by 3, then adding or subtracting 1 isn't the Fibonacci sequence. Two, I don't see the need for recursion here. What exactly is this function supposed to do? If it simply takes a number in the sequence and returns the next number in the sequence, there is no need for recursion. fibo(101) is supposed to return 304, right?

What list are you referring to? A list of all the numbers in the sequence which are less than or equal to 101?
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,373 posts
since Jan 2008
Sep 14th, 2008
0

Re: help needed with recursive fibonacci

1. the reason for me doing it recursively is because its an exercise in recursion.
2. after looking at the sequence of numbers i determined that 1 was the base case.
and if the position of the nth number was even it would multiply the previos number by 3 and subtract 1 if the position of the nth number was odd it would multiply by 3 and add 1.
the assignment was simply to create the method as i have done ( i believe).
in addition to this i wanted to write the program to see the function work....that im not sure how to do it.

the list im referring to is the list of numbers 1, 4 , 11, 34, 101.
I am simply tryin to create a recursive program to find the 6th integer in this list.

Thanks
Reputation Points: 10
Solved Threads: 0
Light Poster
SyLk is offline Offline
27 posts
since Oct 2004
Sep 14th, 2008
0

Re: help needed with recursive fibonacci

Click to Expand / Collapse  Quote originally posted by SyLk ...
1. the reason for me doing it recursively is because its an exercise in recursion.
2. after looking at the sequence of numbers i determined that 1 was the base case.
and if the position of the nth number was even it would multiply the previos number by 3 and subtract 1 if the position of the nth number was odd it would multiply by 3 and add 1.
the assignment was simply to create the method as i have done ( i believe).
in addition to this i wanted to write the program to see the function work....that im not sure how to do it.

the list im referring to is the list of numbers 1, 4 , 11, 34, 101.
I am simply tryin to create a recursive program to find the 6th integer in this list.

Thanks
Then I'd say you want to pass the function n , not nthfibo .
What kind of List are you going to use? It's a matter of personal preference, but I like Vector. You could make this your function:

Java Syntax (Toggle Plain Text)
  1. public static void fibo(Vector <Integer> numbers, int n)

If you're going to have a Vector, you'll need to pass fibo the Vector each recursive call, so you might as well have it be a void function. So if you passed it an empty Vector, the following call:

Java Syntax (Toggle Plain Text)
  1. fibo (numbers, 6);

could result in numbers holding {1, 4, 11, 34, 101, 304}. Is that what you're looking for? I'd rename the function though.

Quote ...
I am simply trying to create a recursive program to find the 6th integer in this list.
So you do want the whole List, right, not just the 6th element?
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,373 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: PROJECT ideas on J2EE
Next Thread in Java Forum Timeline: class return problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC