Maximum Python Int Size Error 3

Asked

Viewed 21 times

-1

I made a simple algorithm in Python 3 that proves that the "If a number n is prime, then 2 n -1 is prime" theorem is false.

However, I wanted him to do it with all the numbers up to number 50. But, it stops at number 31, because 2 31 -1 exceeds the maximum capacity of an int32 in python2 (2147483647), however, I am using Python 3, where the int has no limit, which could be causing this limitation?

My code below:

def eprimo(n):
    if(n == 1):
        return False
    numero = 2
    while numero<n:
        if(n%numero == 0):
            return False
        numero += 1   
    return True
    
    
i = 1
while i<=50:
    if(eprimo(i)):
        print(f'{i} é primo')
        pq = 2**i-1
        if(eprimo(pq) == False):
            print(f'O número {i} prova que o teorema é falso')
    i+=1
print("Acabou a execução")

Note: I have tested on websites that compile python 3, and the error persists

  • 1

    What was the error? Here at Ideone (https://ideone.com/TAV6Dl) he calculated 2**100000 - 1 without problems.

  • 2

    The program does not stop, the problem is that making 2 billion iterations is even time consuming: I tested here only with 2147483647 and it took more than 3 minutes, then patience :-) If you change the algorithm of checking prime number to a more "smart" (ie, one that does not take so long), you will see that the problem is not to burst the "limits of an int" but the execution time. See: https://ideone.com/hIYaRf

  • Absolutely no error, it simply stops running from the 31. I generated a link in the replit in case you want to test: https://replit.com/join/iegpcuhkln-zecabagunha

  • Got it @hkotsubo, so in fact the problem was running time, thank you very much for the help!

  • It’s just that I started to think it was due to the maximum size of the python int, because the last output 2 31 -1 is exactly the same as 2147483647, so I was left with this doubt

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.