#!/usr/bin/env python # http://projecteuler.net/index.php?section=problems&id=21 import math from EulerLibs import MathLibs d = [] for i in range(0,10001): d.append(MathLibs.sumOfDivisors(i)) amicables = [] for i in range(2,10001): try: if d[d[i]] == i and d[i] != i: amicables.append(i) except IndexError: pass answer = sum(amicables) print answer
#!/usr/bin/env python # http://projecteuler.net/index.php?section=problems&id=27 from EulerLibs import MathLibs most_primes = { 'a': 1, 'b': 41, 'primes': 40 } for a in xrange(-999, 1000): for b in xrange(-999, 1000): n = 0 prime_count = 0 while True: p = n**2 + (a * n) + b if p < 2: break elif not MathLibs.isPrime(p): break prime_count += 1 n += 1 values = { 'a': a, 'b': b, 'primes': prime_count } if values['primes'] > most_primes['primes']: most_primes = values answer = most_primes['a'] * most_primes['b'] print answer
#!/usr/bin/env python # http://projecteuler.net/index.php?section=problems&id=3 import math from EulerLibs import MathLibs answer = 0 for i in MathLibs.factors(600851475143): if MathLibs.isPrime(i): answer = i print answer
#!/usr/bin/env python # http://projecteuler.net/index.php?section=problems&id=7 import math from EulerLibs import MathLibs primes = 0 answer = 1; while (primes < 10001): answer += 1 if (MathLibs.isPrime(answer)): primes += 1 print answer
#!/usr/bin/env python # http://projecteuler.net/index.php?section=problems&id=12 import math from EulerLibs import MathLibs print MathLibs.factorial(500) factors = 0 cursor = 0 while (factors <= 500): cursor += 1 answer = MathLibs.triangleNumber(cursor) factors = len(MathLibs.factors(answer)) print answer
#!/usr/bin/env python # http://projecteuler.net/index.php?section=problems&id=20 import math from EulerLibs import MathLibs,StrLibs f100 = str(MathLibs.factorial(100)) answer = StrLibs.sumDigits(f100) print answer
# Find a common denominator of 1 to x # @return int def common_denom(x): resp = 1 while x >= 1: if resp % x: resp *= x x -= 1 return resp common_factor = common_denom(20) answer = 0 for i in MathLibs.factors(common_factor): j = 1 disproven = False while j <= 20 and not disproven: if i % j: disproven = True if j == 20: answer = i j += 1 if answer: break