Skip to content

Instantly share code, notes, and snippets.

@arekusuri
arekusuri / best_route.py
Created August 4, 2014 03:45
Find the maximum total from top to bottom of the triangle below:
mport numpy as np
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
@arekusuri
arekusuri / letter_counts.py
Created August 3, 2014 23:12
If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used? NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contai…
info_less_than20 = { 0:"", 1:"one", 2:"two", 3:"three", 4:"four", 5:"five",
6:"six", 7:"seven", 8:"eight", 9:"nine", 10:"ten",
11:"eleven", 12:"twelve", 13:"thirteen", 14:"forteen", 15:"fifteen",
16:"sixteen", 17:"seventeen", 18:"eighteen", 19:"nineteen",
}
info_biger_than_twenty = { 20:"twenty", 30:"thirty", 40:"forty", 50:"fifty",
60:"sixty", 70:"seventy", 80:"eighty", 90:"ninety",
}
arr = [list(str(x)) for x in range(1000)]
arr = [x[:-2]+["".join(x[-2:])] for x in arr]
@arekusuri
arekusuri / big_number.py
Created August 3, 2014 20:19
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?
def multiply2(arr):
arr = [x*2 for x in arr]
arr.append(0)
for i in range(len(arr)-1):
arr[i+1] += arr[i] / 10
arr[i] = arr[i] % 10
if arr[-1] == 0:
arr = arr[:-1]
return arr
@arekusuri
arekusuri / find_route.py
Created August 3, 2014 19:37
How many such routes are there through a 20×20 grid?
def f(n):
arr = [1]
for i in range(n):
a = [1] * (len(arr) + 1)
for j in range(1, len(arr)):
a[j] = arr[j] + arr[j-1]
arr = a
return sum([x*x for x in arr])
print f(20)
@arekusuri
arekusuri / longest_chain.py
Created August 3, 2014 18:21
Which starting number, under one million, produces the longest chain?
under = 1000000 + 1
info = [0] * (3 * under)
def func(n):
if n == 1:
return 1
if n < len(info):
if info[n]:
return info[n]
t = n/2 if n%2 == 0 else n*3 +1
info[n] = 1 + func(t)
@arekusuri
arekusuri / sum_100_row.py
Last active August 29, 2015 14:04
Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.
import numpy as np
data = """
37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676
89261670696623633820136378418383684178734361726757
28112879812849979408065481931592621691275889832738
44274228917432520321923589422876796487670272189318
@arekusuri
arekusuri / triangle_500.py
Created August 3, 2014 03:50
What is the value of the first triangle number to have over five hundred divisors?
import math
def func(target=500):
n = 1
while True:
n = n+1
total = n * (n+1)/2
count = 2
for i in range(2, int(math.sqrt(total))):
if total%i == 0:
@arekusuri
arekusuri / max_grid_productor.py
Created August 3, 2014 00:11
In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 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 8…
import numpy as np
from itertools import chain
data = """
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
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
@arekusuri
arekusuri / sum_prime.py
Last active August 29, 2015 14:04
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million.
def find_prime_under(n=100):
arr = range(n)
nn = n - 1
for num in arr:
if num > 1:
i = 2
while True:
mul = i*num
i += 1
if mul > nn:
@arekusuri
arekusuri / find_pythagorean.py
Created August 2, 2014 18:33
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.
t = 1000.
def calc_a(b):
a = (t*t - 2*t*b)/2/(t-b)
return a
ab = [(calc_a(b), b) for b in range(1, int(t))]
ab = filter(lambda x: float(int(x[0])) == x[0] and x[0] > 0, ab)
abc = [list(x) + [t-x[0]-x[1]] for x in ab]
productor = [int(x[0] * x[1] * x[2]) for x in abc]