A theory question relating to recursion....
For any recursive problem, if your n gets really really really big, will it always segment fault at some value of n no matter what?
A theory question relating to recursion....
For any recursive problem, if your n gets really really really big, will it always segment fault at some value of n no matter what?
Yes, if it is a real recursion. For recursion the process needs ad least a return address on the stack. This takes stack spaces and thus eventually will lead to StackOverFlow.
If you expect such a large number of recursive calls, then you better aggregate the results.
A recursive sum would be:
Sum of all elements in a sequence =
first element +
sum of all elements in the sequence without the first one;
Instead of this, use a version without recursion
sum = 0;
elementsLeft = complete sequence
while still elements left
{
sum = sum + first element of elementsLeft
elementsLeft = elementsLeft without the first element
}