Increasing recursion fails with: Process finished with exit code -1073741571 (0xC00000FD)

Question:

import sys
sys.setrecursionlimit(1000000000)

def F(n):
    if n == 1:
        return 1
    if n > 1:
        return n * F(n - 1)
print(F(2023)/F(2020))

When the recursion is increased with setrecursionlimit, the following error is returned.

Process finished with exit code -1073741571 (0xC00000FD)

But without increasing the recursion, this code does not work.

How can I fix it?

Asked By: alexunder

||

Answers:

You can increase the recursion up to 2500 since your factorial is less than 2500:

import sys
sys.setrecursionlimit(2500)

def F(n):
    if n == 1:
        return 1
    if n > 1:
        return n * F(n - 1)

But, when you run above code you will get:

enter image description here

Link to doc: doc

So, you have to increase the limit by:

import sys
sys.set_int_max_str_digits(0)

Now, your code will run:

print(F(2023)/F(2020))

8266912626.0

Alternatively, there is another way;

you can use the built in factorial from math module. But, you have to increase the integer limit:

import sys
sys.set_int_max_str_digits(0)
from math import factorial as F
print(F(2023)/F(2020))

#8266912626.0
Answered By: God Is One