segmentation fault in python
Question: How can I run the following program in python 2.7.3

    
    
    import sys
    sys.setrecursionlimit(2 ** 20)
    def f(x):
        if (x==0): return 0
        else: return f(x-1)+1
    print f(200000)
    

This code receives segmentation fault in Ubuntu.


Answer: The Python interpreter runs out of stack space. Like any other process in the
same situation, it is getting killed by the operating system.

You could try increasing the OS stack size limit (`ulimit -c`).

A better approach might be to rewrite your code so that it does not require
recursion this deep (your particular example can be trivially converted into
iteration).

