Stack Overflow error occurs when using recursive fibonacci function

后端 未结 3 1417
Happy的楠姐
Happy的楠姐 2020-12-21 11:12

Here\'s my code and it runs well with values of 400 to 4000 but once it\'s about 4mil, I get stack overflow errors.

Thanks in advance!

public class F         


        
3条回答
  •  轮回少年
    2020-12-21 11:44

    Yes - you're running out of stack space. It's far from infinite, and you're using it up on each recursive call. You're trying to end up with a stack with 4 million stack frames - that's not going to work.

    I suggest you consider an iterative approach. Even if you had an infinite amount of stack, that code would probably not complete before the heat death of the universe. (Think about the complexity of this code...)

提交回复
热议问题