mu chance or much chance ?

日々の戯れ言

大学基礎数学

大学基礎数学を復習するために,以下の本を購入しました.

初めから学べると評判の大学基礎数学微分積分キャンパス・ゼミ

初めから学べると評判の大学基礎数学微分積分キャンパス・ゼミ

初めから学べると評判の大学基礎数学確率統計キャンパス・ゼミ

初めから学べると評判の大学基礎数学確率統計キャンパス・ゼミ

今月中に復習を終えたいです.

Project Euler 19

  • 問題

Problem 19:Counting Sundays
You are given the following information, but you may prefer to do some research for yourself.

1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

  • 解答例
def zeller(year, month, day):
    if(month < 3):
        year -= 1
        month += 12
    return (year + year // 4 - year // 100 + year // 400 + (13 * month + 8 ) // 5 + day) % 7

count = 0
for i in range(1901, 2001):
    for j in range(1, 13):
        if zeller(i, j, 1) == 0:
            count += 1

print(count)

Project Euler 18

  • 問題

Problem 18:Maximum path sum I
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.

3
7 4
2 4 6
8 5 9 3

That is, 3 + 7 + 4 + 9 = 23.

Find the maximum total from top to bottom of the triangle below:

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

  • 解答例
f = open("pe18.txt", "r")
array = []
for line in f:
    text = line.replace('\n', '')
    text = text.replace('\r', '')
    text = text.split()
    temp = [int(i) for i in text]
    array.append(temp)
f.close()

# 初期設定
l = len(array)
result = [0] * l

for i in range(l - 1)[::-1]:
    for j in range(i + 1):
        temp = array[i + 1][j] if array[i + 1][j] > array[i + 1][j + 1] else array[i + 1][j + 1]
        array[i][j] += temp

print(array[0][0])
  • 用意したファイル(pe18.txt)
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

Project Euler 17

  • 問題

Problem 17:Number letter counts
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?

  • 解答例
figure = {  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:"fourteen",
            15:"fifteen",
            16:"sixteen",
            17:"seventeen",
            18:"eighteen",
            19:"nineteen",
            20:"twenty",
            30:"thirty",
            40:"forty",
            50:"fifty",
            60:"sixty",
            70:"seventy",
            80:"eighty",
            90:"ninety",
            100:"hundred",
            1000:"onethousand"}

def num2wordLen(num):
    l = 0
    # 1000の処理
    if num == 1000:
        l += len(figure[1000])
        return l
    # 100〜999の処理
    q100 = num // 100
    r100 = num % 100
    if(q100 > 0):
        l += (len(figure[100]) + len(figure[q100]))
        # and処理
        if(r100 != 0):
            l += 3
    # 1〜99の処理
    if(r100 < 20):
        l += len(figure[r100])
    else:
        q10 = r100 // 10
        r10 = r100 % 10
        l += (len(figure[q10 * 10]) + len(figure[r10]))       
    return l

count = 0
for i in range(1, 1001):
    count += num2wordLen(i)

print(count)