# # For example, when the list is sorted into alphabetical order, COLIN, # which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the # list. So, COLIN would obtain a score of 938 × 53 = 49714. # # What is the total of all the name scores in the file? import eutils.strings as strings import eutils.vectorize as vec import re import time start = time.time() val = dict(zip(list('ABCDEFGHIJKLMNOPQRSTUVWXYZ'), list(range(1, 27)))) f = open('../data/names.txt', 'r') n = "" for line in f: n += line.rstrip() f.close() names = [re.sub("[^A-Z]", "", x) for x in n.split(',')] names.sort() ans = vec.v_sum([vec.v_sum(strings.map_vals(x, val)) * (i + 1) for i, x in enumerate(names)]) end = time.time() print("The answer to Problem 22 is: %s" % ans) print("<< Returned in %s seconds >>" % (end - start))
# Problem 16 # # 2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. # # What is the sum of the digits of the number 2^1000? import eutils.vectorize as vec import time start = time.time() ans = vec.v_sum([int(x) for x in list(str(2 ** 1000))]) end = time.time() print("The answer to Problem 16 is: %s" % ans) print("<< Returned in %s seconds >>" % (end - start))
# Problem 20 # # n! means n × (n − 1) × ... × 3 × 2 × 1 # # For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, and the sum of # the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. # # Find the sum of the digits in the number 100! import eutils.lists as lists import eutils.vectorize as vec import math import time start = time.time() ans = vec.v_sum(lists.l_digits(math.factorial(100))) end = time.time() print("The answer to Problem 20 is: %s" % ans) print("<< Returned in %s seconds >>" % (end - start))
# Problem 13 # # Work out the first ten digits of the sum of the following one-hundred # 50-digit numbers. # # [See 0013.txt] import eutils.vectorize as vec import time start = time.time() f = open('../data/0013.txt', 'r') mat = [int(x) for x in f] f.close() ans = int(str(vec.v_sum(mat))[0:10]) end = time.time() print("The answer to Problem 13 is: %s" % ans) print("<< Returned in %s seconds >>" % (end - start))
# # Using words.txt (right click and 'Save Link/Target As...'), a 16K text # file containing nearly two-thousand common English words, how many are # triangle words? import eutils.strings as strings import eutils.vectorize as vec import re import time start = time.time() tri = dict(zip([int(0.5 * n * (n + 1)) for n in list(range(1, 25))], list(range(1, 25)))) val = dict(zip(list('ABCDEFGHIJKLMNOPQRSTUVWXYZ'), list(range(1, 27)))) f = open('../data/words.txt', 'r') n = "" for line in f: n += line.rstrip() f.close() words = [re.sub("[^A-Z]", "", x) for x in n.split(",")] wvals = [vec.v_sum(strings.map_vals(x, val)) for x in words] ans = len([x for x in wvals if x in tri]) end = time.time() print("The answer to Problem 42 is: %s" % ans) print("<< Returned in %s seconds >>" % (end - start))
# a prime, contains 21 terms, and is equal to 953. # # Which prime, below one-million, can be written as the sum of the most # consecutive primes? import eutils.primes as primes import eutils.vectorize as vec import time start = time.time() ceil = 1000000 p = [2] + [n for n in list(range(3, ceil, 2)) if primes.is_prime(n)] max = [0, 0] for i in range(0, len(p)): j = i + 1 + max[1] ssum = vec.v_sum(p[i:j]) while j <= len(p) and ssum < ceil: if primes.is_prime(ssum): max = [ssum, len(p[i:j])] j += 1 ssum = vec.v_sum(p[i:j]) ans = max[0] end = time.time() print("The answer to Problem 50 is: %s" % ans) print("<< Returned in %s seconds >>" % (end - start))
# # 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 eutils.factors as factors import eutils.vectorize as vec import time start = time.time() abun = [] for n in range(1, 28124): if n < vec.v_sum(factors.l_factors(n)[:-1]): abun.append(n) n = list(range(1,28124)) able = [x + y for i, x in enumerate(abun) for y in abun[i:]] able = dict(zip(able, [0] * len(able))) ans = vec.v_sum([y for y in n if y not in able]) end = time.time() print("The answer to Problem 23 is: %s" % ans) print("<< Returned in %s seconds >>" % (end - start))
# * d4d5d6=635 is divisible by 5 # * d5d6d7=357 is divisible by 7 # * d6d7d8=572 is divisible by 11 # * d7d8d9=728 is divisible by 13 # * d8d9d10=289 is divisible by 17 # # Find the sum of all 0 to 9 pandigital numbers with this property. import eutils.strings as strings import eutils.vectorize as vec import math import time start = time.time() n = 10 digits = list(range(0, n)) check = [1,2,3,5,7,11,13,17] perms = strings.l_permute(digits)[int(math.factorial(n) / n):] perms = [''.join([str(y) for y in x]) for x in perms] ans = 0 for s in perms: pan = vec.v_sum([int(s[i:i + 3]) % d for i, d in enumerate(check)]) if pan == 0: ans += int(s) end = time.time() print("The answer to Problem 43 is: %s" % ans) print("<< Returned in %s seconds >>" % (end - start))