Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Perfect Number formula in Python Question an Mersenne Numbers
#1
Hi all,
Great thanks for this site it helps. Anyway I have found a direct relationship with perfect numbers to Mersenne Numbers. For Instance 6 is a perfect Number and it is divisible by two which equals 3. My current formula is looking at exponents of the number 4. If you want to find Mersenne numbers than put an exponent on 4 double the number and substract 1. However the exponents are staggered at times. but if you do it enough you will land a Mersenne number.
28 is a perfect number divide it by 7 and you get 4.

Lets look at 4^3=64 next 2*64-1=127
Lets look at 4^6=4096 next 2*4096-1=8191
try 4^15

I need some code that will process large perfect numbers. Thanks for reading :)
Quote
#2
see: https://www.w3resource.com/python-exerci...ise-11.php
Quote
#3
(Apr-24-2018, 10:56 AM)Larz60+ Wrote: see: https://www.w3resource.com/python-exerci...ise-11.php
Thanks Larz60,

Now I'm trying to write code for a simple formula 4**y*2-1=
Input y several times with different numbers automated. I believe I need some kinda of exponent loop so I can integrate it with the Perfect Numbers module, but I'm clueless. Plz help.

For you perfect Number people here is the ultimate code:

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(6)
    primes = prime_sieve()
    next(primes)                     #2 is barely even a prime
    for prime in primes:
        if is_mersenne_prime(prime, 2**prime-1):
            yield(2**(2*prime-1)-2**(prime-1))

if __name__ == '__main__':
    for perfect in calculate_perfects():
        print(perfect)
Quote
#4
(Apr-24-2018, 07:46 AM)Pleiades Wrote: Hi all, Great thanks for this site it helps. Anyway I have found a direct relationship with perfect numbers to Mersenne Numbers. For Instance 6 is a perfect Number and it is divisible by two which equals 3. My current formula is looking at exponents of the number 4. If you want to find Mersenne numbers than put an exponent on 4 double the number and substract 1. However the exponents are staggered at times. but if you do it enough you will land a Mersenne number. 28 is a perfect number divide it by 7 and you get 4. Lets look at 4^3=64 next 2*64-1=127 Lets look at 4^6=4096 next 2*4096-1=8191 try 4^15 I need some code that will process large perfect numbers. Thanks for reading :)
Ok I'm getting somewhere I'm finding Mersennes in this code. However can you guys help me plz with a small script which finds the P associated with the mersenne. 2^p-1 thank you.

# Python Program to display the powers of 2 using anonymous function

# Change this value for a different result
terms = 60

# Uncomment to take number of terms from user
#terms = int(input("How many terms? "))

# use anonymous function
result = list(map(lambda x: 4 ** x*2-1, range(terms)))

# display the result

print("The total terms is:",terms)
for i in range(terms):
   print("4 raised to power",i,"is",result[i])
Quote
#5
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.
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to apply VLookup formula jonzee 2 55 Jan-12-2020, 04:16 PM
Last Post: Clunk_Head
  Want to add formula to excel sheet nagu4651 1 173 Dec-26-2019, 10:13 PM
Last Post: keuninkske
  Need help to identify Mersenne Primes, I do need a search pattern. Pleiades 0 130 Dec-03-2019, 11:05 PM
Last Post: Pleiades
  pyxcel_ods formula eslavko 1 98 Nov-20-2019, 01:02 PM
Last Post: ChislaineWijdeven
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 343 May-09-2019, 12:19 PM
Last Post: Pleiades
  Python Formula prophet11 5 541 Apr-17-2019, 07:40 PM
Last Post: perfringo
  Finding perfect numbers BillMcEnaney 6 750 Apr-04-2019, 04:46 AM
Last Post: BillMcEnaney
  Parsing Large Numbers Question amyvaulhausen 5 666 Mar-24-2019, 08:46 AM
Last Post: samsonite
  Help with try and open 6.txt file and print as perfect or not Pleiades 13 956 Jan-03-2019, 10:14 PM
Last Post: Pleiades
  Divide a number by numbers in a list. Wallen 3 1,084 Sep-14-2018, 05:49 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)