Python Forum
Perfect Number formula in Python Question an Mersenne Numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Perfect Number formula in Python Question an Mersenne Numbers
#6
(Apr-26-2018, 04:37 PM)Pleiades Wrote: Hi, in post 4 my question remains unanswered. Is there a way to add a, "for loop division option", here that will find all the P's (as in primes associated with the Mersenne Numbers) in 2^P-1 numbers? Thanks for your help.

I edited the code and I'm finding the P's out of 2p-1 for Mersenne Primes. Can anyone please point me to a fast computer online which runs python for this code; or is anyone up to the challenge to speed up the code. I'm thinking that my laptop is a little slow. Here is some output.
Thanks all for your support and help:)
Quote:3
5
7
13
17
19
31
61
89
107
127
521
607
1279
2203
2281
3217
4253
4423
9689
9941
11213
    
from itertools import count
 
def postponed_sieve():                   # postponed sieve, by Will Ness      
    yield 2; yield 3; yield 5; yield 7;  # original code David Eppstein, 
    sieve = {}                           #   Alex Martelli, ActiveState Recipe 2002
    ps = postponed_sieve()               # a separate base Primes Supply:
    p = next(ps) and next(ps)            # (3) a Prime to add to dict
    q = p*p                              # (9) its sQuare 
    for c in count(9,2):                 # the Candidate
        if c in sieve:               # c's a multiple of some base prime
            s = sieve.pop(c)         #     i.e. a composite ; or
        elif c < q:  
             yield c                 # a prime
             continue              
        else:   # (c==q):            # or the next base prime's square:
            s=count(q+2*p,2*p)       #    (9+6, by 6 : 15,21,27,33,...)
            p=next(ps)               #    (5)
            q=p*p                    #    (25)
        for m in s:                  # the next multiple 
            if m not in sieve:       # no duplicates
                break
        sieve[m] = s                 # original test entry: ideone.com/WFv4f
 
def prime_sieve():                   # postponed sieve, by Will Ness      
    yield 2; yield 3; yield 5; yield 7;  # original code David Eppstein, 
    sieve = {}                           #   Alex Martelli, ActiveState Recipe 2002
    ps = postponed_sieve()               # a separate base Primes Supply:
    p = next(ps) and next(ps)            # (3) a Prime to add to dict
    q = p*p                              # (9) its sQuare 
    for c in count(9,2):                 # the Candidate
        if c in sieve:               # c’s a multiple of some base prime
            s = sieve.pop(c)         #     i.e. a composite ; or
        elif c < q:  
             yield c                 # a prime
             continue              
        else:   # (c==q):            # or the next base prime’s square:
            s=count(q+2*p,2*p)       #    (9+6, by 6 : 15,21,27,33,...)
            p=next(ps)               #    (5)
            q=p*p                    #    (25)
        for m in s:                  # the next multiple 
            if m not in sieve:       # no duplicates
                break
        sieve[m] = s                 # original test entry: ideone.com/WFv4f
 
def mod_mersenne(n, prime, mersenne_prime):
    while n > mersenne_prime:
        n = (n & mersenne_prime) + (n >> prime)
    if n == mersenne_prime:
        return 0
    return n
 
def is_mersenne_prime(prime, mersenne_prime):
    s = 4
    for i in range(prime - 2):
        s = mod_mersenne((s*s - 2), prime, mersenne_prime)
    return s == 0
 
def calculate_perfects():
    yield(2)
    primes = prime_sieve()
    next(primes)                     #2 is barely even a prime
    for prime in primes:
        if is_mersenne_prime(prime, 2**prime-1):
            yield(prime)
                  
if __name__ == '__main__':
    for perfect in calculate_perfects():
        print(perfect)
#edited by Tom E. O'Neil to find Mprimes
Reply


Messages In This Thread
RE: Perfect Number formula in Python Question an Mersenne Numbers - by Pleiades - May-16-2018, 04:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Are there errors in the code for my coin toss systems? Matlibplot is too perfect . Coolkat 0 342 Nov-13-2023, 11:54 AM
Last Post: Coolkat
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,012 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Divide a number by numbers in a list. Wallen 7 7,925 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  Python “Formula” Package: How do I parse Excel formula with a range of cells? JaneTan 1 2,639 Jul-12-2021, 11:09 AM
Last Post: jefsummers
  How do I read in a Formula in Excel and convert it to do the computation in Python? JaneTan 2 2,559 Jul-07-2021, 02:06 PM
Last Post: Marbelous
  Question about formula implementation in general format Alienspecimen 0 1,633 Mar-01-2021, 08:39 PM
Last Post: Alienspecimen
  Perfect numbers Vidar567 2 1,873 Nov-23-2020, 10:29 PM
Last Post: Vidar567
  Applying Moving Averages formula to a number lynnette1983 1 1,996 Sep-29-2020, 10:21 AM
Last Post: scidam
  Runs perfect in Python but fails to print last statement when converted to .exe. Help sunil422 3 2,747 Aug-13-2020, 01:22 PM
Last Post: deanhystad
  Need help to identify Mersenne Primes, I do need a search pattern. Pleiades 0 1,891 Dec-03-2019, 11:05 PM
Last Post: Pleiades

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020