idiomas

Imperative languages.
Syndicate content

Perfect number finder in python

In the interest of greater openness (sharing my inner life with the world and such), here is a bit of python I just wrote to find perfect numbers. After a few minutes of optimization, it is now quite fast and no longer brings OSX to its knees with memory-hungryness:

from math import *
def is_perfect(a):
	sum = 1 
	for i in xrange(2, floor(sqrt(a))):
		if a%i == 0:
			sum = sum + i + a/i
	if sqrt(a)%1 == 0: sum += sqrt(a)
	return sum == a

def find_perfect(limit):
	results = []
	for i in xrange(1, limit):
		j=2**i*(2**(i+1)-1)
		if is_perfect(j):
			results.append(j)