1

Here's a part of my code that is relevant to the problem solving

public long Recursiv(){
    Fibonacci fibo = new Fibonacci(this.getNumar());
    long fibnum;

    if(this.getNumar() == 0) return 0;
    if(this.getNumar() <= 2) return 1;
    else
        fibnum = (fibo.Recursiv()-1) + (fibo.Recursiv()-2);
        return fibnum;

}

It is part of the Fibonacci class that I've created which has a constructor, a getter and setter methods.

My goal is to implement and print even fibonacci numbers in the console e.g. 2, 8, 34...etc.

In the main method, whenever I apply the method on the object I receive a java.lang.StackOverflowError message with the issue that the line which makes the assignment is the problem.

What should I do?

2
  • Run in the debugger, name methods starting with lowercase letters and post complete code examples. Commented Dec 7, 2015 at 19:16
  • 3
    What do you think this: fibo.Recursiv()-1 is doing? Commented Dec 7, 2015 at 19:18

2 Answers 2

2

The calculation of the Fibonacci sequence can be done by implementing the method as follows.

public long Recursiv(int n)
{
    if (0 == n)
        return 0;
    else if (1 == n)
        return 1;
    else
        return this.Recursiv(n - 1) + this.Recursiv(n - 2)
}

However this would require changing of the method's signature.

Edit:

Apparently I've misunderstood the question a bit; the desired argument is given in the constructor and the method should use instances with smaller arguments for evaluation. The method could also be implemented as follows.

public long Recursiv()
{
    if(this.getNumar() == 0)
        return 0;
    if(this.getNumar() <= 2)
        return 1;
    else
        return new Fibonacci(this.getNumar() - 2).Recursiv()
             + new Fibonacci(this.getNumar() - 1).Recursiv();
}
Sign up to request clarification or add additional context in comments.

3 Comments

True, I was attempting to do it without the input, I was wondering if there is another way.
No worries, that's interesting so basically at the return it is creating new instances of the Fibonnacci class, am I right?
Yes, this apparently works, but it is a rather complicated way of doing the evaluation.
2

You need a method that calculates the nth fibonacci number and returns the result — so you have to tell it which number in the fibonacci sequence you want.
That requires a parameter.

public long fib(int n) {
    ...
}

Now a fibonacci number is the sum of the two previous fibonacci numbers; that is

fib(6) = fib(5) + fib(4)
// or more generally
fib(n) = fib(n-1) + fib(n-2)

so (without writing the whole thing for you) your method needs to accept and pass the necessary parameters:

public long fib(int n) {
    if (n == 0) return 0;
    //  etc. -- handle the defined/degenerate cases
    return fib(n-1) + fib(n-2)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.