Problem 11
maxNum = 0 s="""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 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""" mat = [[int(y) for y in x.split(" ")] for x in s.split("\n")] def prd4(lst): return lst[0]*lst[1]*lst[2]*lst[3] for r in range(20): for c in range(20-3): maxNum = max(prd4(mat[r][c:c+4]), maxNum) # horizontal for r in range(20-3): for c in range(20): maxNum = max(prd4([x[c] for x in mat[r:r+4]]), maxNum) # vertical for r in range(20-3): for c in range(20-3): maxNum = max(mat[r][c]*mat[r+1][c+1]*mat[r+2][c+2]*mat[r+3][c+3], maxNum) for r in range(20-3): for c in range(20-3): maxNum = max(mat[r+3][c]*mat[r+2][c+1]*mat[r+1][c+2]*mat[r][c+3], maxNum) print(maxNum)
Problem 12
def divisors(n): ret = [] for i in range(int(n**0.5)): if n%(i+1) == 0: ret += [i+1, int(n/(i+1))] return ret i=2 triangleNum=1 while len(divisors(triangleNum)) < 500: triangleNum += i i+=1 #triangleNum = 76576500
Problem 13
和をstrにして最初の10文字をとるだけ
Problem 14
In [78]: def collatz(n):...: if n&1:...: return 3*n+1...: else:...: return int(n/2)...:...: def collatzList(m):...: lst = [m]...: while m != 1:...: m = collatz(m)...: lst.append(m)...: return lstIn [80]: maxLen = 0...: maxNum = 0...: for i in range(3, 1000000, 1):...: lst = collatzList(i)...: if maxLen < len(lst):...: maxLen = len(lst)...: maxNum = i...: maxNum, maxLenOut[80]: (837799, 525)
結構計算時間がかかるので注意
Problem 15
40P20=13784652882020! ⇒ eval("*".join([str(x+1) for x in range(20)]))
!を定義して愚直に計算する。
Problem 16
eval("+".join(str(2**1000)))
evalを使うと記述量は削減できる。
Problem 17
英語という自然言語を扱う問題なのでパス
Problem 18
In [90]: s="""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"""In [91]: pyramid = [[int(y) for y in x.split(" ")] for x in s.split("\n")]In [92]: for j in range(15-1, 0, -1):...: for i in range(j):...: pyramid[j-1][i] += max(pyramid[j][i], pyramid[j][i+1])...: pyramid
ピラミッドの底辺から順番に、最大値を計算していく。最小単位の3つから考えれば最大値は自明になる。
Problem 19
In [158]: n = 0...: from datetime import datetime as dt...: for i in range(1901, 2000+1, 1):...: for j in range(1, 12+1, 1):...: if dt(i, j, 1).weekday() == 6:...: n += 1...: nOut[158]: 171
車輪の再開発を防ぐという意味で、datetimeを使うのはギリギリ反則ではないと思う…
Problem 20
階乗を定義しておくIn [160]: def factorial(n):...: return eval("*".join([str(x) for x in range(n, 1, -1)]))In [162]: eval("+".join(str(factorial(100))))Out[162]: 648
とりあえず19個!