Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I fix it?

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading