# Problem 5 # ========= # 2520 is the smallest number that can be divided by each of the numbers # from 1 to 10 without any remainder. # What is the smallest positive number that is evenly divisible by all of # the numbers from 1 to 20? # Answer: 232792560 calculated in 1.09672546387e-05 seconds from euler import runtime, lcm from functools import reduce def lcm_range(r): return reduce(lcm, xrange(1, r+1)) runtime(lcm_range, 20)
# Answer: 1074 calculated in 3.38554382324e-05 seconds from euler import runtime def problem_18(p): for x in xrange(len(p)-1, 0, -1): for y in xrange(x): p[x-1][y] += max(p[x][y], p[x][y+1]) return p[0] pyramid = [ [75], [95, 64], [17, 47, 82], [18, 35, 87, 10], [20, 04, 82, 47, 65], [19, 01, 23, 75, 03, 34], [88, 02, 77, 73, 07, 63, 67], [99, 65, 04, 28, 06, 16, 70, 92], [41, 41, 26, 56, 83, 40, 80, 70, 33], [41, 48, 72, 33, 47, 32, 37, 16, 94, 29], [53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14], [70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57], [91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48], [63, 66, 04, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31], [04, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 04, 23] ] runtime(problem_18, pyramid)
# Problem 29 # ========== # Consider all integer combinations of a^b for 2 <= a <= 5 and 2 <= b <= 5: # 2^2=4, 2^3=8, 2^4=16, 2^5=32 # 3^2=9, 3^3=27, 3^4=81, 3^5=243 # 4^2=16, 4^3=64, 4^4=256, 4^5=1024 # 5^2=25, 5^3=125, 5^4=625, 5^5=3125 # If they are then placed in numerical order, with any repeats removed, we # get the following sequence of 15 distinct terms: # 4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125 # How many distinct terms are in the sequence generated by a^b for 2 <= a <= # 100 and 2 <= b <= 100? # Answer: 9183 calculated in 0.0198509693146 seconds from euler import runtime def distinct_terms(r): return len({a**b for a in xrange(2, r+1) for b in xrange(2, r+1)}) runtime(distinct_terms, 100)
# ========== # Pentagonal numbers are generated by the formula, P[n]=n(3n-1)/2. The first # ten pentagonal numbers are: # 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ... # It can be seen that P[4] + P[7] = 22 + 70 = 92 = P[8]. However, their # difference, 70 - 22 = 48, is not pentagonal. # Find the pair of pentagonal numbers, P[j] and P[k], for which their sum # and difference are pentagonal and D = |P[k] - P[j]| is minimised; what is # the value of D? # Answer: 5482660 calculated in 0.122126102448 seconds from euler import runtime def problem_44(): p = {x * (3 * x - 1) / 2 for x in xrange(1000, 3000)} for x in p: for y in p: if x + y in p and abs(y - x) in p: return abs(y - x) runtime(problem_44)
# Problem 206 # =========== # Find the unique positive integer whose square has the form # 1_2_3_4_5_6_7_8_9_0, # where each "_" is a single digit. # Answer: 1389019170 calculated in 1.95045495033 seconds from euler import runtime def problem_206(): for x in xrange(150000001, 1, -2): if str(x**2)[::2] == '123456789': return x*10 runtime(problem_206)
# It is well known that if the square root of a natural number is not an # integer, then it is irrational. The decimal expansion of such square roots # is infinite without any repeating pattern at all. # The square root of two is 1.41421356237309504880..., and the digital sum # of the first one hundred decimal digits is 475. # For the first one hundred natural numbers, find the total of the digital # sums of the first one hundred decimal digits for all the irrational square # roots. # Answer: 40886 calculated in 0.00938415527344 seconds from euler import runtime from decimal import Decimal, getcontext from math import sqrt getcontext().prec = 102 def problem_80(s=0): for x in xrange(2, 100): if not sqrt(x) % 1: continue d = str(Decimal(x).sqrt()*10**99) s += sum([int(x) for x in d[:100]]) return s runtime(problem_80)
# Problem 113 # =========== # Working from left-to-right if no digit is exceeded by the digit to its # left it is called an increasing number; for example, 134468. # Similarly if no digit is exceeded by the digit to its right it is called a # decreasing number; for example, 66420. # We shall call a positive integer that is neither increasing nor decreasing # a "bouncy" number; for example, 155349. # As n increases, the proportion of bouncy numbers below n increases such # that there are only 12951 numbers below one-million that are not bouncy # and only 277032 non-bouncy numbers below 10^10. # How many numbers below a googol (10^100) are not bouncy? # Answer: 51161058134250 calculated in 5.69820404053e-05 seconds from euler import runtime, binomial def problem_113(): return sum([binomial(110, 100), binomial(109, 100), -1002]) runtime(problem_113)
# Problem 3 # ========= # The prime factors of 13195 are 5, 7, 13 and 29. # What is the largest prime factor of the number 600851475143 ? # Answer: 6857 calculated in 0.0001540184021 seconds from euler import runtime, prime_factorization def largest_prime_factor(n): return max(prime_factorization(n)) runtime(largest_prime_factor, 600851475143)
# The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, # and 5, giving the pandigital, 918273645, which is the concatenated product # of 9 and (1,2,3,4,5). # What is the largest 1 to 9 pandigital 9-digit number that can be formed as # the concatenated product of an integer with (1,2, ... , n) where n > 1? # Answer: 932718654 calculated in 0.00693202018738 seconds from euler import runtime, pandigital def concated_product(n, r): s = '' for x in xrange(r+1): s += str(n*x) return int(s) def largest_concated_pandigital_product(largest=0): for x in xrange(10000, 9000, -1): for i in xrange(3): c = concated_product(x, i) if pandigital(c): if c > largest: largest = c return largest runtime(largest_concated_pandigital_product)
# * d[2]d[3]d[4]=406 is divisible by 2 # * d[3]d[4]d[5]=063 is divisible by 3 # * d[4]d[5]d[6]=635 is divisible by 5 # * d[5]d[6]d[7]=357 is divisible by 7 # * d[6]d[7]d[8]=572 is divisible by 11 # * d[7]d[8]d[9]=728 is divisible by 13 # * d[8]d[9]d[10]=289 is divisible by 17 # Find the sum of all 0 to 9 pandigital numbers with this property. # Answer: 16695334890 calculated in 3.18974900246 seconds from euler import runtime from itertools import permutations def problem_43(s=0): p = permutations('1234567890') for y in p: y = ''.join(y) if (not int(y[1:4]) % 2 and not int(y[2:5]) % 3 and not int(y[3:6]) % 5 and not int(y[4:7]) % 7 and not int(y[5:8]) % 11 and not int(y[6:9]) % 13 and not int(y[7:]) % 17): s += int(y) return s runtime(problem_43)
# F[3] = 2 # F[4] = 3 # F[5] = 5 # F[6] = 8 # F[7] = 13 # F[8] = 21 # F[9] = 34 # F[10] = 55 # F[11] = 89 # F[12] = 144 # The 12th term, F[12], is the first term to contain three digits. # What is the first term in the Fibonacci sequence to contain 1000 digits? # Answer: 4782 calculated in 0.0314209461212 seconds from euler import runtime from itertools import count def fibonacci_digits(n): x, y = 0, 1 for i in count(): if len(str(x)) == n: return i x, y = y, x+y runtime(fibonacci_digits, 1000)
for x in xrange(1, len(n)-adj-1): p = 1 for i in xrange(1, adj+1): p *= int(n[x+i]) if p > m: m = p return m num = ('73167176531330624919225119674426574742355349194934' '96983520312774506326239578318016984801869478851843' '85861560789112949495459501737958331952853208805511' '12540698747158523863050715693290963295227443043557' '66896648950445244523161731856403098711121722383113' '62229893423380308135336276614282806444486645238749' '30358907296290491560440772390713810515859307960866' '70172427121883998797908792274921901699720888093776' '65727333001053367881220235421809751254540594752243' '52584907711670556013604839586446706324415722155397' '53697817977846174064955149290862569321978468622482' '83972241375657056057490261407972968652414535100474' '82166370484403199890008895243450658541227588666881' '16427171479924442928230863465674813919123162824586' '17866458359124566529476545682848912883142607690042' '24219022671055626321111109370544217506941658960408' '07198403850962455444362981230987879927244284909188' '84580156166097919133875499200524063689912560717606' '05886116467109405077541002256983155200055935729725' '71636269561882670428252483600823257530420752963450') runtime(max_adjacent_product, num, 13)
# Problem 24 # ========== # A permutation is an ordered arrangement of objects. For example, 3124 is # one possible permutation of the digits 1, 2, 3 and 4. If all of the # permutations are listed numerically or alphabetically, we call it # lexicographic order. The lexicographic permutations of 0, 1 and 2 are: # 012 021 102 120 201 210 # What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, # 4, 5, 6, 7, 8 and 9? # Answer: 2783915460 calculated in 0.109308958054 seconds from euler import runtime from itertools import permutations def nth_lexi_perm(r, n, c=0): x = permutations(xrange(r), r) for i in x: if c == n-1: return ''.join(str(x) for x in i) c += 1 runtime(nth_lexi_perm, 10, 1000000)
# Problem 36 # ========== # The decimal number, 585 = 1001001001[2] (binary), is palindromic in both # bases. # Find the sum of all numbers, less than one million, which are palindromic # in base 10 and base 2. # (Please note that the palindromic number, in either base, may not include # leading zeros.) # Answer: 872187 calculated in 0.553943872452 seconds from euler import runtime, palindrome def palindrome_base_2_and_10(r, s=0): for x in xrange(r): if palindrome(x) and palindrome(bin(x)[2:]): s += x return s runtime(palindrome_base_2_and_10, 1000000)
# 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. # Answer: 4179871 calculated in 0.436667919159 seconds from euler import runtime, divisors def problem_23(s=0): n = set() for x in xrange(1, 28123): if sum(divisors(x)) > x: n.add(x) for y in n: if x-y in n: break else: s += x return s runtime(problem_23)
# Problem 21 # ========== # 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. # Answer: 31626 calculated in 0.08455991745 seconds from euler import runtime, divisors def d(n): return sum(divisors(n)) def sum_amicable(m): return sum([a for a in xrange(1, m) if d(d(a)) == a and a != d(a)]) runtime(sum_amicable, 10000)
# 15 = 3 x 5 # The first three consecutive numbers to have three distinct prime factors # are: # 644 = 2^2 x 7 x 23 # 645 = 3 x 5 x 43 # 646 = 2 x 17 x 19. # Find the first four consecutive integers to have four distinct prime # factors. What is the first of these numbers? # Answer: 134043 calculated in 0.314765930176 seconds from euler import runtime, prime_factorization from itertools import count def dpf(n): return len(set(prime_factorization(n))) def problem_47(): for x in count(100000): if (dpf(x) == 4 and dpf(x+1) == 4 and dpf(x+2) == 4 and dpf(x+3) == 4): return x runtime(problem_47)
# Problem 15 # ========== # Starting in the top left corner of a 2x2 grid, and only being able to move # to the right and down, there are exactly 6 routes to the bottom right # corner. # How many such routes are there through a 20x20 grid? # Answer: 137846528820 calculated in 5.96046447754e-06 seconds from euler import runtime, binomial runtime(binomial, 40, 20)
# Problem 9 # ========= # A Pythagorean triplet is a set of three natural numbers, a < b < c, for # which, # a^2 + b^2 = c^2 # For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. # There exists exactly one Pythagorean triplet for which a + b + c = 1000. # Find the product abc. # Answer: 31875000 calculated in 0.0185961723328 seconds from euler import runtime from math import sqrt def prod_of_triplet(s): for a in xrange(s/2): for b in xrange(s/2): if a**2 + b**2 == (s-a-b)**2: return a*b*(s-a-b) runtime(prod_of_triplet, 1000)
# ├────┼──────────────────┼──────┼───────────┤ # │ 7 │ 1,2,3,4,5,6 │ 6 │ 1.1666... │ # ├────┼──────────────────┼──────┼───────────┤ # │ 8 │ 1,3,5,7 │ 4 │ 2 │ # ├────┼──────────────────┼──────┼───────────┤ # │ 9 │ 1,2,4,5,7,8 │ 6 │ 1.5 │ # ├────┼──────────────────┼──────┼───────────┤ # │ 10 │ 1,3,7,9 │ 4 │ 2.5 │ # └────┴──────────────────┴──────┴───────────┘ # It can be seen that n=6 produces a maximum n/φ(n) for n ≤ 10. # Find the value of n ≤ 1,000,000 for which n/φ(n) is a maximum. # Answer: 510510 calculated in 2.19345092773e-05 seconds from euler import runtime, prime_sieve def problem_69(r, m=1): # find the product of consecutive primes under r primes = prime_sieve(50) for x in primes: if m * x > r: return m m *= x runtime(problem_69, 1000000)
# By converting each letter in a word to a number corresponding to its # alphabetical position and adding these values we form a word value. For # example, the word value for SKY is 19 + 11 + 25 = 55 = t[10]. If the word # value is a triangle number then we shall call the word a triangle word. # Using [1]words.txt, a 16K text file containing nearly two-thousand common # English words, how many are triangle words? # Answer: 162 calculated in 0.00173592567444 seconds from euler import runtime from string import ascii_uppercase def triangle_words(file, c=0): triangles = {x*(x+1)/2 for x in xrange(1000)} value = {c: ascii_uppercase.index(c)+1 for c in ascii_uppercase} with open(file, 'r') as f: words = list(f.read().replace('"', '').split(',')) for word in words: s = 0 for char in word: s += value[char] if s in triangles: c += 1 return c runtime(triangle_words, 'txt/words.txt')
# Problem 10 # ========== # The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. # Find the sum of all the primes below two million. # Answer: 142913828922 calculated in 0.0908539295197 seconds from euler import runtime, prime_sieve def sum_of_primes(m): return sum(prime_sieve(m)) runtime(sum_of_primes, 2000000)
# Let us list the factors of the first seven triangle numbers: # 1: 1 # 3: 1,3 # 6: 1,2,3,6 # 10: 1,2,5,10 # 15: 1,3,5,15 # 21: 1,3,7,21 # 28: 1,2,4,7,14,28 # We can see that 28 is the first triangle number to have over five # divisors. # What is the value of the first triangle number to have over five hundred # divisors? # Answer: 76576500 calculated in 0.253170967102 seconds from euler import runtime, prime_factorization, n_divisors from itertools import count def triangle_divisors(n, t=1): for x in count(2): if n_divisors(t) > n: return t t += x runtime(triangle_divisors, 500)
# The number, 197, is called a circular prime because all rotations of the # digits: 197, 971, and 719, are themselves prime. # There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, # 71, 73, 79, and 97. # How many circular primes are there below one million? # Answer: 55 calculated in 3.2831389904 seconds from euler import runtime, prime, prime_sieve def circular_prime(n): for i in xrange(len(str(n))): if not prime(int(str(n)[i:] + str(n)[:i])): return False return True def count_circular_primes(r, c=0): for x in prime_sieve(r): if circular_prime(x): c += 1 return c runtime(count_circular_primes, 1000000)
'97142617910342598647204516893989422179826088076852\n' '87783646182799346313767754307809363333018982642090\n' '10848802521674670883215120185883543223812876952786\n' '71329612474782464538636993009049310363619763878039\n' '62184073572399794223406235393808339651327408011116\n' '66627891981488087797941876876144230030984490851411\n' '60661826293682836764744779239180335110989069790714\n' '85786944089552990653640447425576083659976645795096\n' '66024396409905389607120198219976047599490197230297\n' '64913982680032973156037120041377903785566085089252\n' '16730939319872750275468906903707539413042652315011\n' '94809377245048795150954100921645863754710598436791\n' '78639167021187492431995700641917969777599028300699\n' '15368713711936614952811305876380278410754449733078\n' '40789923115535562561142322423255033685442488917353\n' '44889911501440648020369068063960672322193204149535\n' '41503128880339536053299340368006977710650566631954\n' '81234880673210146739058568557934581403627822703280\n' '82616570773948327592232845941706525094512325230608\n' '22918802058777319719839450180888072429661980811197\n' '77158542502016545090413245809786882778948721859617\n' '72107838435069186155435662884062257473692284509516\n' '20849603980134001723930671666823555245252804609722\n' '53503534226472524250874054075591789781264330331690') def problem_13(n): return sum([int(i) for i in n.split('\n')]) runtime(sum_nums, num)
# Problem 1 # ========= # If we list all the natural numbers below 10 that are multiples of 3 or 5, # we get 3, 5, 6 and 9. The sum of these multiples is 23. # Find the sum of all the multiples of 3 or 5 below 1000. # Answer: 233168 calculated in 0.000159025192261 seconds from euler import runtime def sum_of_threes_and_fives(m): return sum([x for x in xrange(m) if not x % 3 or not x % 5]) runtime(sum_of_threes_and_fives, 1000)
# 123, 124, 125, 134, 135, 145, 234, 235, 245, and 345 # In combinatorics, we use the notation, ^5C[3] = 10. # In general, # ^nC[r] = n! ,where r <= n, n! = n x(n-1) x...x3x2x1, and 0! = 1 # r!(n-r)! # It is not until n = 23, that a value exceeds one-million: ^23C[10] = # 1144066. # How many, not necessarily distinct, values of ^nC[r], for 1 <= n <= 100, # are greater than one-million? # Answer: 4075 calculated in 0.0622220039368 seconds from euler import runtime, binomial def problem_53(r, m, c=0): for x in xrange(r+1): for y in xrange(r+1): if binomial(x, y) > m: c += 1 return c runtime(problem_53, 100, 1000000)
# Problem 4 # ========= # A palindromic number reads the same both ways. The largest palindrome made # from the product of two 2-digit numbers is 9009 = 91 x 99. # Find the largest palindrome made from the product of two 3-digit numbers. # Answer: 906609 calculated in 0.456642866135 seconds from euler import runtime, palindrome def largest_palindrome(m, largest=0): for x in xrange(100, m): for y in xrange(100, m): p = x*y if palindrome(p) and p > largest: largest = p return largest runtime(largest_palindrome, 1000)
# A number chain is created by continuously adding the square of the digits # in a number to form a new number until it has been seen before. # For example, # 44 -> 32 -> 13 -> 10 -> 1 -> 1 # 85 -> 89 -> 145 -> 42 -> 20 -> 4 -> 16 -> 37 -> 58 -> 89 # Therefore any chain that arrives at 1 or 89 will become stuck in an # endless loop. What is most amazing is that EVERY starting number will # eventually arrive at 1 or 89. # How many starting numbers below ten million will arrive at 89? # Answer: 8581146 calculated in 37.9294171333 seconds from euler import runtime, sum_digits def problem_92(r, c89=0): for x in xrange(1, r): while x not in [1, 89]: x = sum_digits(x, 2) if x == 89: c89 += 1 return c89 runtime(problem_92, 10000000)
# Problem 7 # ========= # By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see # that the 6th prime is 13. # What is the 10 001st prime number? # Answer: 104743 calculated in 0.00478291511536 seconds from euler import runtime, prime_sieve def nth_prime(i, m): return prime_sieve(m)[i] runtime(nth_prime, 10000, 110000)