Example #1
0
File: p21.py Project: vkuzo/euler
"""
Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
 If d(a) = b and d(b) = a, where a  b, then a and b are an 
 amicable pair and each of a and b are called amicable numbers.

For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; 
therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000.
"""

import math
from p12 import factor, getPrimes
import unittest

PRIMES = getPrimes(10001)

def sumProperDivisors(n, PRIMES):
	"""
	Returns the sum of the proper divisors of n
	"""
	factors = factor(n, PRIMES)
	s = 1
	for x in factors:
		s *= (math.pow(x, factors[x]+1)-1) / (x - 1)
	return int(s) - n

class testProblem(unittest.TestCase):
	def setUp(self):
		pass
		
Example #2
0
File: p23.py Project: vkuzo/euler
As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, 
the smallest number that can be written as the sum of two abundant numbers is 24. 
By mathematical analysis, it can be shown that all integers greater than 28123 
can be written as the sum of two abundant numbers. However, this upper limit 
cannot be reduced any further by analysis even though it is known that the 
greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
"""

import time
from p12 import getPrimes
from p21 import sumProperDivisors
import unittest

PRIMES = getPrimes(28124)

def isAbundant(n):
	"""
	Returns true if n is abundant
	"""
	return sumProperDivisors(n, PRIMES) > n

class testProblem(unittest.TestCase):
	def setUp(self):
		pass
		
	def testSumProperDivisors(self):
		self.assertEquals(28, sumProperDivisors(28, PRIMES))
		
if __name__ == '__main__':