0

I have a small snippet of code that computes the factorial of a number using a nested function shown below:

import sys
rec_limit = 10 ** 6
num = 10 ** 5

sys.setrecursionlimit(rec_limit)

def factorial(n):
    if n == 1:
        return 1
    ans = n * factorial(n - 1)
    return ans

print(factorial(num))

For smaller values of num, the program works fine, but for very large values such as the one in the program, a recursion limit is thrown. To avoid this, I used the sys.setrecursionlimit() method to increase this limit. However, now I receive a Segmentation fault (core dumped) error on Linux, while on windows, the program just returns without any error.

Please could you explain why this is happening and how I might avoid this problem?

Thanks!

Andy Krugs
  • 31
  • 3

0 Answers0