MIPS Recursive Fibonacci Sequence

I’m having trouble dealing with stacks recursively in MIPS. I get the concept, but my program isn’t reacting as I mean it to. My goal is to take user input as n and print the Fibonacci number at n. What I have so far is below. (I’m fairly certain the problem is in the actual … Read more

Java recursive Fibonacci sequence

In fibonacci sequence each item is the sum of the previous two. So, you wrote a recursive algorithm. So, Now you already know fibonacci(1)==1 and fibonacci(0) == 0. So, you can subsequently calculate the other values. Now, And from fibonacci sequence 0,1,1,2,3,5,8,13,21…. we can see that for 5th element the fibonacci sequence returns 5. See here for Recursion Tutorial.

Exception in thread “main” java.lang.StackOverflowError

Your algorithm is fine. However int is too small for your computations, it fails for this input: At some point integer overflows to negative value and your implementation goes crazy, recursing infinitely. Change int num to long num and you’ll be fine – for some time. Later you’ll need BigInteger. Note that according to Wikipedia on Collatz conjecture (bold mine): The longest progression for … Read more