def pe0012(): print 'Project Euler Problem 12 https://projecteuler.net/problem=12' p = eulerlib.sieve_of_atkin(65500) n = 3 Dn = 2 cnt = 0 while cnt <= 500: n = n + 1 n1 = n if n1 % 2 == 0: n1 = n1 / 2 Dn1 = 1 for i in range(0, p[-1]): if p[i] * p[i] > n1: Dn1 *= 2 break exponent = 1 while n1 % p[i] == 0: exponent += 1 n1 /= p[i] if exponent > 1: Dn1 *= exponent if n1 == 1: break cnt = Dn * Dn1 Dn = Dn1 print 'Answer is ' + str(n*(n-1)/2)
def pe0003(): print 'Project Euler Problem 3 https://projecteuler.net/problem=3' num = 600851475143 pf = eulerlib.sieve_of_atkin(int(math.sqrt(num))) for p in reversed(pf): if num % p == 0: print 'Answer is ' + str(p) return
def pe0026(): print 'Project Euler Problem 26 https://projecteuler.net/problem=26' primes = eulerlib.sieve_of_atkin(1000)[::-1] ans = 0 for p in primes: x = 1 while (10 ** (x + 2) // p) % 1000 != (10 ** (2 * x + 2) // p) % 1000: x += 1 if x + 1 == p: ans = p break print 'Answer is ' + str(ans)
def pe0050(): print 'Project Euler Problem 50 https://projecteuler.net/problem=50' primes = eulerlib.sieve_of_atkin(4000) for i in range(len(primes) - 1, 1, -1): start = 0 while True: prime_sum = sum(primes[start:start + i]) if prime_sum > 1000000: break if eulerlib.is_prime(prime_sum): print 'Answer is %d' % prime_sum return start += 1
def pe0049(): print 'Project Euler Problem 49 https://projecteuler.net/problem=49' primes = [p for p in eulerlib.sieve_of_atkin(9999) if p > 1000] for i in range(0, len(primes) - 1): count = 1 prime_perm = [primes[i]] for j in range(i + 1, len(primes)): if sorted(str(primes[i])) == sorted(str(primes[j])): count += 1 prime_perm.append(primes[j]) if count == 3 and abs(prime_perm[1] - prime_perm[0]) == abs(prime_perm[2] - prime_perm[1]): print 'Answer is ' + ''.join(str(c) for c in prime_perm) return
def pe0077(): print 'Project Euler Problem 77 https://projecteuler.net/problem=77' primes = eulerlib.sieve_of_atkin(100) print primes ways = [0] * 100 ways[0] = 1 for p in primes: for z in range(p, 100): ways[z] = ways[z] + ways[z - p] for w in ways: if w >= 5000: print 'Answer is %d' % ways.index(w) break
def pe0046(): print 'Project Euler Problem 46 https://projecteuler.net/problem=46' primes = eulerlib.sieve_of_atkin(10000) i = 33 keep_trying = True while keep_trying: j = 0 keep_trying = False while i >= primes[j]: if eulerlib.is_twice_square(i - primes[j]): keep_trying = True break j += 1 i += 2 print 'Answer is %d' % (i - 2)
def pe0037(): print 'Project Euler Problem 37 https://projecteuler.net/problem=37' primes = eulerlib.sieve_of_atkin(1000000) trunc_primes = [] possible_primes = [] for i in primes: if i < 100 or len(set(['0', '2', '4', '5', '6', '8']).intersection(set(str(i)))) == 0: possible_primes.append(i) for p in possible_primes: for z in range(1, len(str(p))): if int(str(p)[:z]) not in primes or int(str(p)[z:]) not in primes: break if z == len(str(p)) - 1: trunc_primes.append(p) print trunc_primes print 'Answer is %d' % sum(trunc_primes)
def pe0069(): print 'Project Euler Problem 69 https://projecteuler.net/problem=69' # To have higher n / phi(n), phi(n) should be smaller # phi(n) = n * sum(1 - 1/p), where p is all prime factors of n # hence if we have more prime factors for a number then that will have less phi(n) limit = 1000001 primes = eulerlib.sieve_of_atkin(50) prod = 1 pnum = [] for p in primes: prod *= p if prod < limit: pnum.append(p) else: break print 'Answer is ' + str(reduce(mulop, pnum, 1))
def pe0035(): print 'Project Euler Problem 35 https://projecteuler.net/problem=35' circular_primes = [2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97] primes = eulerlib.sieve_of_atkin(1000000) possible_primes = [] for i in primes: if i > 100 and len(set(['0', '2', '4', '5', '6', '8']).intersection(set(str(i)))) == 0: possible_primes.append(i) for p in possible_primes: r = list(eulerlib.get_num_digits_rotated(p)) for j in range(0, len(r)): if r[j] not in primes: break if j == len(r) - 1: for z in r: circular_primes.append(z) print 'Answer is %d' % len(set(circular_primes))
def pe0070(): print 'Project Euler Problem 70 https://projecteuler.net/problem=70' # To have lower n / phi(n), phi(n) should be bigger. # Only when n is prime it's bigger with phi(n) = n - 1, but n - 1 will not be permutation of n # phi(n) = n * sum(1 - 1/p), where p is all prime factors of n # Hence if we have less prime factors for a number then that will have higher phi(n), hence go for 2 prime factors. # phi(p1 x p2) = phi(p1) x phi(p2) = (p1 - 1) x (p2 - 1) # It's rational to start from sqrt(10^7) limit = 10 ** 7 primes = eulerlib.sieve_of_atkin(2 * int(math.sqrt(limit))) ans = 1 min_num = float(10 ** 7) for p1, p2 in combinations(primes, 2): if p1 * p2 < limit: n = p1 * p2 phi = (p1 - 1) * (p2 - 1) tmp = float(n) / phi if sorted(str(n)) == sorted(str(phi)) and tmp < min_num: min_num = tmp ans = n print 'Answer is ' + str(ans)
def pe0060(): print 'Project Euler Problem 60 https://projecteuler.net/problem=60' # Trial and error to find the uppoerbound as 10000 for sensible execution time primes = eulerlib.sieve_of_atkin(10000) # removed 2 & 5 from primes since numbers ending with 2 & 5 are not primes primes.remove(2) primes.remove(5) def isPrimePair(x, y): if eulerlib.is_prime(int(str(x) + str(y))) and eulerlib.is_prime(int(str(y) + str(x))): return True else: return False def find5thpair(primes): for i in range(len(primes)): for j in range(i + 1, len(primes)): pairs = [] if isPrimePair(primes[i], primes[j]): pairs.append(primes[i]) pairs.append(primes[j]) for a in range(j + 1, len(primes)): if isPrimePair(pairs[0], primes[a]) and isPrimePair(pairs[1], primes[a]): pairs.append(primes[a]) for b in range(a + 1, len(primes)): if isPrimePair(pairs[0], primes[b]) and isPrimePair(pairs[1], primes[b]) and \ isPrimePair(pairs[2], primes[b]): pairs.append(primes[b]) for c in range(b + 1, len(primes)): if isPrimePair(pairs[0], primes[c]) and \ isPrimePair(pairs[1], primes[c]) and isPrimePair(pairs[2], primes[c]) and \ isPrimePair(pairs[3], primes[c]): pairs.append(primes[c]) return pairs result = find5thpair(primes) print 'Answer is %d' % sum(result)
def pe0027(): print 'Project Euler Problem 27 https://projecteuler.net/problem=27' primes = eulerlib.sieve_of_atkin(1000)[::-1] for i in range(0, len(primes)): primes.append(-primes[i]) cons_big = 0 coeff = [0, 0] for b in primes: for a in range(-999, 1000): n, pc = 0, 0 while True: t = (n * n) + (a * n) + b n += 1 if t < 0: break elif eulerlib.is_prime(t): pc += 1 if cons_big < pc: cons_big = pc coeff[0] = a coeff[1] = b else: break print 'Answer is %d' % (coeff[0] * coeff[1])
def pe0010(): print 'Project Euler Problem 10 https://projecteuler.net/problem=10' p = eulerlib.sieve_of_atkin(2000000) print 'Answer is ' + str(sum(p))