s2 = s1 - g * 100 for f in range(s2 // 50 + 1): s3 = s2 - f * 50 for e in range(s3 // 20 + 1): s4 = s3 - e * 20 for d in range(s4 // 10 + 1): s5 = s4 - d * 10 for c in range(s5 // 5 + 1): s6 = s5 - c * 5 for b in range(s6 // 2 + 1): s7 = s6 - b * 2 for a in range(s7 + 1): s8 = s7 - a if s8 == 0: r += 1 return r # set up problem problem_url = 'https://projecteuler.net/problem=31' p = EulerProblem('Coin sums', problem_url, 200) p.problem = problem p.date_solved = '2021-02-14' if __name__ == '__main__': # test p.run(n=1) # get average time p.run()
# instead of the prime^degree, save it as the prime repeated degree times nonprimes_divs3 = [] primes_used = [] for i in nonprimes_divs2: if i.get('prime') not in primes_used: primes_used.append(i.get('prime')) nonprimes_divs3 = nonprimes_divs3 + ([i.get('prime')] * i.get('degree')) # find the product of this list, then divide by the values in primes that also appear in that list nonprimes_divs_product = np.product(np.array(nonprimes_divs3)) for p in primes: if p in nonprimes_divs3: nonprimes_divs_product = nonprimes_divs_product / p return int(np.prod(primes) * nonprimes_divs_product) # set up problem problem_url = 'https://projecteuler.net/problem=5' p = EulerProblem('Smallest multiple', problem_url, 20) p.problem = problem p.date_solved = '2021-01-29' if __name__ == '__main__': p.run(10, n=1) assert p.solution == 2520, 'Result incorrect' # test p.run(n=1) # get average time p.run()
85786944089552990653640447425576083659976645795096 66024396409905389607120198219976047599490197230297 64913982680032973156037120041377903785566085089252 16730939319872750275468906903707539413042652315011 94809377245048795150954100921645863754710598436791 78639167021187492431995700641917969777599028300699 15368713711936614952811305876380278410754449733078 40789923115535562561142322423255033685442488917353 44889911501440648020369068063960672322193204149535 41503128880339536053299340368006977710650566631954 81234880673210146739058568557934581403627822703280 82616570773948327592232845941706525094512325230608 22918802058777319719839450180888072429661980811197 77158542502016545090413245809786882778948721859617 72107838435069186155435662884062257473692284509516 20849603980134001723930671666823555245252804609722 53503534226472524250874054075591789781264330331690 """ problem_url = 'https://projecteuler.net/problem=13' p = EulerProblem('Large sum', problem_url, data) p.problem = problem p.date_solved = '2021-02-01' if __name__ == '__main__': # test p.run(n=1) # get average time p.run()
from eulertools import EulerProblem from math import factorial def problem(args:int) -> int: """Sum up individual terms of a factorial-ed number N. """ s = [int(x) for x in str(factorial(args))] return sum(s) # set up problem problem_url = 'https://projecteuler.net/problem=20' p = EulerProblem('Factorial digit sum', problem_url, 100) p.problem = problem p.date_solved = '2021-02-12' if __name__ == '__main__': p.run(10, n=1) assert p.solution == 27, 'Result incorrect' # test p.run(n=1) # get average time p.run()
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 """ problem_url = 'https://projecteuler.net/problem=11' p = EulerProblem('Largest product in a grid', problem_url, (4, data)) p.problem = problem p.date_solved = '2021-01-31' if __name__ == '__main__': # test p.run(n=1) # get average time p.run()
from eulertools import EulerProblem import numpy as np def problem(args: int) -> int: """Find A - B where A = (sum of i for natural numbers i to limit) ^ 2 B = (sum of i^2 for natural numbers i to limit) """ A = (args * (args + 1) / 2)**2 B = np.dot(np.arange(1, args + 1), np.arange(1, args + 1)) return int(A - B) # set up problem problem_url = 'https://projecteuler.net/problem=6' p = EulerProblem('Sum square difference', problem_url, 100) p.problem = problem p.date_solved = '2021-01-29' if __name__ == '__main__': p.run(10, n=1) assert p.solution == 2640, 'Result incorrect' # test p.run(n=1) # get average time p.run()
sieve = np.ones(n // 3 + (n % 6 == 2), dtype=np.bool) for i in range(1, int(n**0.5) // 3 + 1): if sieve[i]: k = 3 * i + 1 | 1 sieve[k * k // 3::2 * k] = False sieve[k * (k - 2 * (i & 1) + 4) // 3::2 * k] = False return np.r_[2, 3, ((3 * np.nonzero(sieve)[0][1:] + 1) | 1)] def problem(args: int) -> int: """Find the nth prime. Simplified by using bounds on the limit needed to find that prime. source: https://en.wikipedia.org/wiki/Prime_number_theorem """ return sum(prime_list(args)) # set up problem problem_url = 'https://projecteuler.net/problem=10' p = EulerProblem('Summation of primes', problem_url, 2000000) p.problem = problem p.date_solved = '2021-01-30' if __name__ == '__main__': p.run(10, n=1) assert p.solution == 17, 'Result incorrect' # test p.run(n=1) # get average time p.run()
from eulertools import EulerProblem def problem(args: int) -> int: """Find pythagorean triple a, b, c where a + b + c = N """ for b in range(1, args): for a in range(1, b + 1): c = args - a - b if a**2 + b**2 == c**2 and a < b < c: return a * b * c # set up problem problem_url = 'https://projecteuler.net/problem=9' p = EulerProblem('Special Pythagorean triplet', problem_url, 1000) p.problem = problem p.date_solved = '2021-01-29' if __name__ == '__main__': # test p.run(n=1) # get average time p.run()
from eulertools import EulerProblem import numpy as np def problem(args:int) -> int: """Sum of number divisible by 3 and 5. Numbers divisible by both are counted in both, so remove the sum of them.. """ n = np.arange(args) n = np.add(1, n) x = n[n % 3 == 0] y = n[n % 5 == 0] z = n[n % (5*3) == 0] return sum(x) + sum(y) - sum(z) # set up problem problem_url = 'https://projecteuler.net/problem=1' p = EulerProblem('Multiples of 3 and 5', problem_url, 1000 - 1) p.problem = problem p.date_solved = '2021-01-29' if __name__ == '__main__': # test the given input p.run(10 - 1, n=1) assert p.solution == 23, 'Result incorrect' # test p.run(n=1) # get average time p.run()
y = (x % 1000 - x % 100) / 100 if 1 <= y <= 9: n += letter_counts[y] + letter_counts[100] return n def problem(args: int) -> int: """Count number of letters when numbers are written out up to N. This only works up to N = 1000. """ s = 0 for i in range(1, args + 1): s += letter_count(i) return s # set up problem problem_url = 'https://projecteuler.net/problem=17' p = EulerProblem('Number letter counts', problem_url, 1000) p.problem = problem p.date_solved = '2021-02-11' if __name__ == '__main__': p.run(5, n=1) assert p.solution == 19, 'Result incorrect' # test p.run(n=1) # get average time p.run()
from eulertools import EulerProblem from math import log # fibonacci sequence formula for n digit number f = lambda n: (n * log(10) + log(5 ** .5)) / log((1 + 5 ** .5) / float(2)) def problem(args:int) -> int: """Finds N-digit Fibonacci number. Derived on paper, see here for a similar one: https://blog.dreamshire.com/solutions/project_euler/project-euler-problem-025-solution/ """ return int(f(args - 1)) + 1 # set up problem problem_url = 'https://projecteuler.net/problem=25' p = EulerProblem('1000-digit Fibonacci number', problem_url, 1000) p.problem = problem p.date_solved = '2021-03-06' if __name__ == '__main__': p.run(3, n=1) assert p.solution == 12, 'Result incorrect' # test p.run(n=1) # get average time p.run()
return False i += 6 return True def problem(args: int) -> int: """Starting at sqrt of n (the highest limit to search for divisors), search for largest prime that divides n. """ limit = int(args**.5) for i in reversed(range(limit)): if args % i == 0: if is_prime(i): return i # set up problem problem_url = 'https://projecteuler.net/problem=3' p = EulerProblem('Largest prime factor', problem_url, 600851475143) p.problem = problem p.date_solved = '2021-01-29' if __name__ == '__main__': # test the given input p.run(13195, n=1) assert p.solution == 29, 'Result incorrect' # test p.run(n=1) # get average time p.run()
"""Count number of Sundays that occur on first month each year during the 20th century. """ month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] month_days_leap = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] year = 1901 day = 366 r = 0 for y in range(100): adjusted_days = month_days if (y + year) % 4 == 0 and (y + year) % 400 != 0: adjusted_days = month_days_leap if (y + year) % 400 == 0: adjusted_days = month_days_leap for m in adjusted_days: r += 1 if lands_on_weekday(day) == 0 else 0 day += m return r # set up problem problem_url = 'https://projecteuler.net/problem=19' p = EulerProblem('Counting Sundays', problem_url, None) p.problem = problem p.date_solved = '2021-02-12' if __name__ == '__main__': # test p.run(n=1) # get average time p.run()
# build list of abundant numbers abnts = [i for i in range(1, limit) if sum_proper_divisors(i) > i] # create list of flags sum_of_abnts = [False] * limit # mark elements in the list that are abundant sums for a in range(len(abnts) + 1): for b in range(a): # use a-1 because adjusting for 0 element t = abnts[a - 1] + abnts[b] if t < limit: sum_of_abnts[t] = True # sum any element in the list that is false for i in range(len(sum_of_abnts)): if not sum_of_abnts[i]: r += i return r # set up problem problem_url = 'https://projecteuler.net/problem=23' p = EulerProblem('Non-abundant sums', problem_url, None) p.problem = problem p.date_solved = '2021-03-05' if __name__ == '__main__': # test p.run(n=1) # get average time p.run()
# set up problem data = """ 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 09 70 98 73 93 38 53 60 04 23 """ problem_url = 'https://projecteuler.net/problem=18' p = EulerProblem('Maximum path sum I', problem_url, data) p.problem = problem p.date_solved = '2021-02-12' if __name__ == '__main__': # test p.run(n=1) # get average time p.run()
"""Find sum of amicable numbers up to N. """ div_list = {} r = 0 # get the divisor sums of values of 1 to N for i in range(1, args + 1): t = get_divisor_sum(i) if 1 < t <= 10000: div_list[i] = t # for each key (K) in div_list, look for its value (V) in div_list (as a key J) # if found, check that J's value W is K and that K != V # in symbols, check that dict[J] (= W) == K and dict[K] (= V) == J for i in div_list: if div_list[i] in div_list: if div_list[div_list[i]] == i and div_list[i] != i: r += i return r # set up problem problem_url = 'https://projecteuler.net/problem=21' p = EulerProblem('Amicable numbers', problem_url, 10000) p.problem = problem p.date_solved = '2021-02-13' if __name__ == '__main__': # test p.run(n=1) # get average time p.run()
from eulertools import EulerProblem def problem(args: int) -> int: """Sum of digits of 2 ^ N. """ s = 0 for i in str(2**args): s += int(i) return s # set up problem problem_url = 'https://projecteuler.net/problem=16' p = EulerProblem('Power digit sum', problem_url, 1000) p.problem = problem p.date_solved = '2021-02-10' if __name__ == '__main__': # test p.run(n=1) # get average time p.run()
sieve[k * (k - 2 * (i & 1) + 4) // 3::2 * k] = False return np.r_[2, 3, ((3 * np.nonzero(sieve)[0][1:] + 1) | 1)] def problem(args: int) -> int: """Find the nth prime. Simplified by using bounds on the limit needed to find that prime. source: https://en.wikipedia.org/wiki/Prime_number_theorem """ limit = int(args * log(args) + args * log(log(args)) + 1) primes = prime_list(limit) if len(primes) >= args: return primes[args - 1] else: return -1 # set up problem problem_url = 'https://projecteuler.net/problem=7' p = EulerProblem('10001st prime', problem_url, 10001) p.problem = problem p.date_solved = '2021-01-29' if __name__ == '__main__': p.run(6, n=1) assert p.solution == 13, 'Result incorrect' # test p.run(n=1) # get average time p.run()
62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450 """ problem_url = 'https://projecteuler.net/problem=8' p = EulerProblem('Largest product in a series', problem_url, (13, data)) p.problem = problem p.date_solved = '2021-01-29' if __name__ == '__main__': p.run([4, data], n=1) assert p.solution == 5832, 'Result incorrect' # test p.run(n=1) # get average time p.run()
from eulertools import EulerProblem from scipy.special import comb def problem(args: int) -> int: """Find number of paths on a grid N x N from top left to bottom right. Can only move down or right. Solution uses nCr (n Choose r) because the total path length is always N x 2 and there are always N moves down and N moves right. So, nCr finds number of ways r objects can be chosen from n total. """ return comb(args * 2, args, exact=True) # set up problem problem_url = 'https://projecteuler.net/problem=15' p = EulerProblem('Lattice paths', problem_url, 20) p.problem = problem p.date_solved = '2021-02-10' if __name__ == '__main__': # test p.run(n=1) # get average time p.run()
- Min of values to search through: 902 * 902. - 902 is the smallest integer > 900 divisible by 11. Since the largest palindrome contains at least 1 number divisible by 11, this lower bound makes sense. """ x = 999 y = 990 # largest value < 1000 divisible by 11 # search for palindrome while not is_palindrome(x * y) and x != 902: y -= 1 # decrement y until it reaches 902 # if y gets to 902, reset y then decrement x if y == 902: y = 990 x -= 1 return x * y # set up problem problem_url = 'https://projecteuler.net/problem=4' p = EulerProblem('Largest palindrome product', problem_url, None) p.problem = problem p.date_solved = '2021-01-29' if __name__ == '__main__': assert is_palindrome(250052), '1. Result incorrect' assert not is_palindrome(15), '2. Result incorrect' # test p.run(n=1) # get average time p.run()
def problem(args: int) -> int: """Create a fibonacci sequence up to a given limit. Take the sum of any even terms. """ a, b, c = 1, 2, 0 result = [a] while c <= args: c = a + b a = b b = c result.append(a) result = np.array(result) return sum(result[result % 2 == 0]) # set up problem problem_url = 'https://projecteuler.net/problem=2' p = EulerProblem('Even Fibonacci numbers', problem_url, 4000000 - 1) p.problem = problem p.date_solved = '2021-01-29' if __name__ == '__main__': # test the given input p.run(89, n=1) check = np.array([1, 2, 3, 5, 8, 13, 21, 34, 55, 89]) assert p.solution == sum(check[check % 2 == 0]), 'Result incorrect' # test p.run(n=1) # get average time p.run()
# determine number of times p can divide y, y/p, y/p/p, etc. # this finds power of the prime divisor exp = 1 while y % p == 0: exp += 1 y = y / p if exp > 1: Dy = Dy * exp if y == 1: break ndivs = Dx * Dy Dx = Dy n += 1 return tri(n - 2) # set up problem problem_url = 'https://projecteuler.net/problem=12' p = EulerProblem('Highly divisible triangular number', problem_url, 500) p.problem = problem p.date_solved = '2021-02-09' if __name__ == '__main__': p.run(5, n=1) assert p.solution == 28, 'Result incorrect' # test p.run(n=1) # get average time p.run()
c = 1 # keep track of length while j != 1: if j in lens: # subtract 1 since current value is j # when j was calculated before, it included itself # as part of the chain length c += lens[j] - 1 break c += 1 j = j // 2 if j % 2 == 0 else 3 * j + 1 maxlen = max(c, maxlen) r = i if maxlen == c else r # save the chain lengths if i not in lens: lens[i] = c i += 1 return r # set up problem problem_url = 'https://projecteuler.net/problem=14' p = EulerProblem('Longest Collatz sequence', problem_url, 1000000) p.problem = problem p.date_solved = '2021-02-10' if __name__ == '__main__': # test p.run(n=1) # get average time p.run()
# number of possible unique combinations after fixing the first element p = factorial(len(remaining) - 1) # count the number of possible combinations # stop when we have gone over the target while s <= limit and c < 10: s += p c += 1 # since s and c have been incremented, need to decrement for # the actual answer r += str(remaining[c - 1]) remaining.pop(c - 1) position = s - p r += str(remaining[0]) return r # set up problem problem_url = 'https://projecteuler.net/problem=24' p = EulerProblem('Lexicographic permutations', problem_url, (1000000, 9)) p.problem = problem p.date_solved = '2021-03-05' if __name__ == '__main__': p.run((6, 2), n=1) assert p.solution == '210', 'Result incorrect' # test p.run(n=1) # get average time p.run()
t += ord(s[i]) - 64 return t def problem(args: str) -> int: """For each word given, score it, and return the total sum of all scores. """ r = 0 data = read_data(args) for i in range(len(data)): r += (i + 1) * score(data[i]) return r # set up problem data_url = 'https://projecteuler.net/project/resources/p022_names.txt' data = get(data_url).text problem_url = 'https://projecteuler.net/problem=22' p = EulerProblem('Names scores', problem_url, data) p.problem = problem p.date_solved = '2021-02-13' if __name__ == '__main__': assert score('COLIN') == 53, 'Result incorrect' # test p.run(n=1) # get average time p.run()