Python Perfect Numbers

2024/11/15 1:23:42

So I am supposed to write a Python program that will identify and print all the perfect numbers in some closed interval [ 2, n ], one per line. We only have to use nested while loops/ if-else statements. I did it somehow using a for loop, but can't figure out the same using a while loop. I'd appreciate your help if you could show me how to translate my code into a while loop. Thanks guys. Here's what I have:

limit = int(input("enter upper limit for perfect number search: "))for n in range(2, limit + 1):sum = 0for divisor in range(1, n):if not n % divisor:sum += divisorif sum == n:print(n, "is a perfect number")
Answer

Here is a (somewhat more efficient) sieve version:

# search all numbers in [2..limit] for perfect numbers
# (ones whose proper divisors sum to the number)
limit = int(input("enter upper limit for perfect number search: "))# initialize - all entries are multiples of 1
#   (ignore sieve[0] and sieve[1])
sieve = [1] * (limit + 1)n = 2
while n <= limit:# check nif sieve[n] == n:print(n, "is a perfect number")# add n to all k * n where k > 1kn = 2 * nwhile kn <= limit:sieve[kn] += nkn += nn += 1

Running it to 10000 finds

6 is a perfect number
28 is a perfect number
496 is a perfect number
8128 is a perfect number

and factorizing those shows an interesting pattern:

6          3 * 2                         (  4 - 1) * (  4 / 2)
28         7 * 2 * 2                     (  8 - 1) * (  8 / 2)
496       31 * 2 * 2 * 2 * 2             ( 32 - 1) * ( 32 / 2)
8128     127 * 2 * 2 * 2 * 2 * 2 * 2     (128 - 1) * (128 / 2)

where the first factor (3, 7, 31, 127) is a prime which is one less than a power of two, and it is multiplied by half that same power of two. Also, the powers involved are prime (2**2, 2**3, 2**5, 2**7).

In fact Euclid proved that (2**p - 1) * 2**(p - 1) is a perfect number if 2**p - 1 is prime, which is only possible (though not assured) if p is prime. Euler went further, proving that all even perfect numbers must be of this form.

This suggests an incredibly more efficient version - I am going to go ahead and use for loops, feel free to rewrite it without. First, we need a source of primes and an is_prime test:

def primes(known_primes=[7, 11, 13, 17, 19, 23, 29]):"""Generate every prime number in ascending order"""# 2, 3, 5 wheelyield from (2, 3, 5)yield from known_primes# The first time the generator runs, known_primes#   contains all primes such that  5 < p < 2 * 3 * 5# After each wheel cycle the list of known primes#   will be added to.# We need to figure out where to continue from,#   which is the next multiple of 30 higher than#   the last known_prime:base = 30 * (known_primes[-1] // 30 + 1)new_primes = []while True:# offs is chosen so  30*i + offs cannot be a multiple of 2, 3, or 5for offs in (1, 7, 11, 13, 17, 19, 23, 29):k = base + offs    # next prime candidatefor p in known_primes:if not k % p:# found a factor - not primebreakelif p*p > k:# no smaller prime factors - found a new primenew_primes.append(k)breakif new_primes:yield from new_primesknown_primes.extend(new_primes)new_primes = []base += 30def is_prime(n):for p in primes():if not n % p:# found a factor - not primereturn Falseelif p * p > n:# no factors found - is primereturn True

then the search looks like

# search all numbers in [2..limit] for perfect numbers
# (ones whose proper divisors sum to the number)
limit = int(input("enter upper limit for perfect number search: "))for p in primes():pp = 2**pperfect = (pp - 1) * (pp // 2)if perfect > limit:breakelif is_prime(pp - 1):print(perfect, "is a perfect number")

which finds

enter upper limit for perfect number search: 2500000000000000000
6 is a perfect number
28 is a perfect number
496 is a perfect number
8128 is a perfect number
33550336 is a perfect number
8589869056 is a perfect number
137438691328 is a perfect number
2305843008139952128 is a perfect number

in under a second ;-)

https://en.xdnf.cn/q/71766.html

Related Q&A

Counting consecutive 1s in NumPy array

[1, 1, 1, 0, 0, 0, 1, 1, 0, 0]I have a NumPy array consisting of 0s and 1s like above. How can I add all consecutive 1s like below? Any time I encounter a 0, I reset.[1, 2, 3, 0, 0, 0, 1, 2, 0, 0]I ca…

python 3 replacement for dircache?

Before I go reinventing the wheel, can anyone tell me if theres a drop-in (or semi-drop-in) replacement for the single-line statement:allfiles = dircache.listdir(.)

AES_128_CTR encryption by openssl and PyCrypto

Wondering the right way to convert a AES_128_CTR encryption by openssl to PyCrypto.First, I did an encryption by openssl as following:openssl enc -aes-128-ctr -in input.mp4 -out output.openssl.mp4 -K 7…

How can i determine the exact size of a type used by python

>>> sys.getsizeof(int) 436 #? does this mean int occupies 436 bytes .>>> sys.getsizeof(1) 12 #12 bytes for int object, is this the memory requirement.I thought int in python is repre…

Python list.clear complexity [duplicate]

This question already has answers here:Python list.clear() time and space complexity?(4 answers)Closed 2 years ago.What is the complexity of the Python 3 method list.clear() ?It is not given here: ht…

Unresolved import org.python / working with jython and java?

Im using Eclipse and I"m trying to create a java program that can run my python code. Im following the guidelines on this page: http://jythonpodcast.hostjava.net/jythonbook/en/1.0/JythonAndJavaInt…

elegant unpacking variable-length tuples

A real, if silly problem:https://github.com/joshmarshall/tornadorpc/blob/master/tornadorpc/base.pydef start_server(handlers, ...):...for (route, handler) in handlers:...Normally "handlers" is…

Extracting data from a 3D scatter plot in matplotlib

Im writing an interface for making 3D scatter plots in matplotlib, and Id like to access the data from a python script. For a 2D scatter plot, I know the process would be:import numpy as np from matpl…

How does Pythonic garbage collection with numpy array appends and deletes?

I am trying to adapt the underlying structure of plotting code (matplotlib) that is updated on a timer to go from using Python lists for the plot data to using numpy arrays. I want to be able to lower …

What is the default if I install virtualenv using pip and pip3 respectively?

I used sudo pip install virtualenv, then when I run virtualenv ENV in a directory, I get a Python 2 virtual enviroment.If I use pip3 install virtualenv to install virtualenv again, will it override the…