Skip to content

Instantly share code, notes, and snippets.

@halloleo
Last active August 29, 2015 13:59
Show Gist options
  • Save halloleo/10705896 to your computer and use it in GitHub Desktop.
Save halloleo/10705896 to your computer and use it in GitHub Desktop.
find "vollkommene zahlen" in python
#!/usr/bin/env python
"""
find "vollkommene zahlen" in python
---
from https://en.wikipedia.org/wiki/Perfect_number:
"In number theory, a perfect number is a positive integer that is equal to
the sum of its proper positive divisors, that is, the sum of its positive
divisors excluding the number itself"
---
despite the "-II" in the file name this version is rather a crude 1st draft of a "brut force" algorithm...
@author: halloleo
"""
import math
def factors(n, foundFactors):
ff = foundFactors
for i in xrange(2, n/2+1):
if n % i == 0:
if not i in ff:
ff.append(i)
ff.append(1)
return ff
def show(ff, n):
print
print "---", ff , "---"
print "sum:", sum(ff)
def loopOver():
n = 2
while True:
ff = factors(n, [])
if sum(ff) == n:
show(ff, n)
n += 1
if __name__ == "__main__":
# flush print statements
import os, sys
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
# do the job
loopOver()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment