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.

public static int fibo(int nthfibo)
 {
      if (nthfibo == 1)
        return 1;
      else if (nthfibo != 1 && nthfibo % 2 ==0)
       {
          return fibo(nthfibo) *3-1;
        }
       else
       {
          return fibo(nthfibo)*3+1;	
        }
		
  }

Dani AI

Generated

The sequence given is not the usual Fibonacci; it follows this rule: a1 = 1, and for n >= 2

  • if n is even: an = 3 * a{n-1} + 1
  • if n is odd: an = 3 * a{n-1} - 1

Verify: a2 = 31+1 = 4, a3 = 34-1 = 11, a4 = 311+1 = 34, a5 = 334-1 = 101, so a6 = 3*101+1 = 304. This corrects the sign/order confusion in the original post.

A simple recursive function that returns the nth term (no list needed) looks like this:

public static int seqTerm(int n) {
    if (n < 1) throw new IllegalArgumentException("n must be >= 1");
    if (n == 1) return 1;
    int prev = seqTerm(n - 1);
    return (n % 2 == 0) ? 3 * prev + 1 : 3 * prev - 1;
}

Call it from main to print the 6th term:

public static void main(String[] args) {
    System.out.println(seqTerm(6)); // prints 304
}

If the goal is to produce the whole sequence (as suggested), pass a mutable List<Integer> to a recursive helper that appends terms until the requested length is reached. That keeps each recursive step using the last value rather than recomputing multiple branches. Note: the original code called the function with the same parameter (e.g. fibo(n)) and therefore never reduced the problem size — that causes infinite recursion. The recursive solution above makes one smaller call per step, giving O(n) time and depth n. For very large n prefer an iterative loop to avoid deep call stacks since Java does not guarantee tail-call optimization.

Recommended Answers

All 3 Replies

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?

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

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:

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:

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.

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.