[Python] Perfect Numbers and Mersenne Primes - Python

Discussão em 'Python' iniciado por Stack, Abril 9, 2025.

  1. Stack

    Stack Membro Participativo

    I've written a program which works out the even perfect numbers for all Mersenne Primes from 1-1000, by using ((2^n)-1)(2^(n-1)) where n is a Mersenne Prime number.

    This is the program:

    def PrimeFinder(PotPrime):
    PlaceNum=1
    for x in range (int(PotPrime**0.5)):
    PlaceNum=PlaceNum+1
    if int(PotPrime/PlaceNum) == (PotPrime/PlaceNum):
    return False
    return True


    TrialNum = 1

    for x in range (1000):

    if PrimeFinder(TrialNum) == True:

    if PrimeFinder((2**TrialNum)-1) == True:
    print(TrialNum,"is the Mersenne Prime for the perfect number:",(2**(TrialNum-1))*((2**TrialNum)-1))

    TrialNum = TrialNum+1


    This program works fine, up until somewhere where 32 < TrialNum < 60, as it correctly identifies that 31 is a Mersenne Prime, however it does not for 61 (and all numbers greater than it).

    I was wondering if Python simply cannot do calculations that large, or whether there is a flaw in either my understanding Mersenne Primes or programming.

    Continue reading...

Compartilhe esta Página