Question
What are the possible ways to solve a java.lang.StackOverflowError caused by an recursive algorithm?
Example
I'm trying to solve Project Euler problem 14 and decided to try it with a recursive algorithm. However, the program stops with an java.lang.StackOverflowError. Understandibly. The algorithm indeed overflowed the stack.
Solutions
So I was wondering: what standard ways are there to solve a java.lang.StackOverflowError assuming your recurisve algorithm was written correctly and would always end up overflowing the stack? Two concepts that came to mind were:
- a) tail recursion
- b) iterative algorithm
Are ideas a) and b) correct? Are there other options?