-3

I was originally solving the Project Euler problem #2 which computes even numbers of the fibonacci sequence up to 4,000,000. I completed this and then I wanted to increase the maximum number so that it would continue infinitely, producing results more slowing as it took longer to compute.

I got a Stack Overflow Error and wondered if there was an alternative solution for the code so that it would be able to continue running, albeit slowly, without encountering a stack overflow error.

Can someone please provide assistance in refactoring my solution? Thanks.

The relevant code for this problem has been provided below.

public static void main(String[] args) {
    fibonacci(1,2,2);
  }

  private static void fibonacci(long first, long second, long sum) {
       long number = first + second;       
         if (number % 2 == 0){ //Checks for even number
           sum = sum + number;
         }
         System.out.println(sum);
         fibonacci(second, number, sum);
         // Produces the following error:
         // Exception in thread "main" java.lang.StackOverflowError
  }
Hiphop03199
  • 729
  • 3
  • 16

4 Answers4

1

You're problem is that you don't have a terminating condition to stop the recursion, you are keep on going until your run out of stack space.

The general format of a recursive function in pseudo code is:

return-typo function(some, values)
{
  if(got to the end of recursion based on some values)
  {
    return something;
  }
  else
  {
    // Recurse
    return function(changes to some values);
  }
}

NOTE: This isn't a hard and fast rule, just a general outline.

Sean
  • 60,939
  • 11
  • 97
  • 136
  • Thanks, I'm not looking to have a boundary condition because I would like to calculate infinitely. – Hiphop03199 Apr 27 '16 at 14:32
  • @Hiphop03199 - You can't count infinitely If nothing else your data type, `long`, will overflow at some point. – Sean Apr 27 '16 at 14:34
1

This calculates fibonacci memorizing 2 numbers

private static BigDecimal fibonacci(int n) {

        BigDecimal cur = new BigDecimal(1);
        BigDecimal prev = new BigDecimal(0);

        //System.out.println(0+") "+1);

        for (int i=1; i<n; i++) {
            final BigDecimal next = cur.add(prev);
            prev = cur;
            cur = next;
            //System.out.println(i+") "+next);
        }

        return cur;
    }
Exceptyon
  • 1,584
  • 16
  • 23
0

The following is functionally the same as your code, just without the recursive call; as such, it will run indefinitely without a StackOverflowError:

  private static void fibonacci(long first, long second, long sum) {
    while(true) {
      long number = first + second;       
      if (number % 2 == 0){ //Checks for even number
        sum = sum + number;
      }
      System.out.println(sum);

      first = second;
      second = number;
    }
  }

However, as noted by others, it will run literally indefinitely, as there is nothing to break the loop.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

You have two alternatives:

  • either use an iterative version of your algorithm, for example something like this
  • or increase your stack size when running Java, more details
Community
  • 1
  • 1
slayful
  • 31
  • 3