a stack overflow is when the stack expands father than expected and crashes into the program data space. stack space is used when variables are created and functions are called.
1) reduce the number of variables that are created every single time the function is called. create the variables and pass to each function the variables to use so that no variables are created within the recursive function. there is still some overhead when the function is created.
2) rewrite the function so that is not recursive (use a while loop).