よしたく blog

ほぼ週刊で記事を書いています

【Project Euler】Problem 19 Counting SundaysをPythonで解く

この問題をPythonで解いた。

#19 Counting Sundays - Project Euler

日本語の問題文はこちら

次の情報が与えられている. 1900年1月1日は月曜日である. 9月, 4月, 6月, 11月は30日まであり, 2月を除く他の月は31日まである. 2月は28日まであるが, うるう年のときは29日である. うるう年は西暦が4で割り切れる年に起こる. しかし, 西暦が400で割り切れず100で割り切れる年はうるう年でない. 20世紀(1901年1月1日から2000年12月31日)中に月の初めが日曜日になるのは何回あるか?

Problem 19 - PukiWiki

from datetime import date

ans=0
for year in range(1901,2001):
    for month in range(1,13):
        if date(year,month,1).weekday()==6:
            ans+=1
print(ans)

~/.config/git/ignoreでグローバルなgitignoreを設定してファイルを除外する

背景

.DS_Store のようなファイルを毎回.gitignoreに書くのがめんどくさくなり、グローバルに設定できないか調べました。

解決方法

  1. ~/.config/gitignoreファイルを作る
  2. .gitignoreと同様に記述する
.DS_Store

これだけで完了です。

Patterns which a user wants Git to ignore in all situations (e.g., backup or temporary files generated by the user’s editor of choice) generally go into a file specified by core.excludesFile in the user’s ~/.gitconfig. Its default value is $XDG_CONFIG_HOME/git/ignore. If $XDG_CONFIG_HOME is either not set or empty, $HOME/.config/git/ignore is used instead.

あらゆる場面で Git に無視させたいパターン (例えば、バックアップファイルやエディタで生成した一時ファイルなど) は、~/.gitconfig の core.excludesFile で指定するファイルに格納します。このファイルのデフォルト値は $XDG_CONFIG_HOME/git/ignore です。XDG_CONFIG_HOME が設定されていないか空の場合は、代わりに $HOME/.config/git/ignore が使用されます。

git-scm.com

別の解決方法

別の解決方法もあります。 ~/.gitignore_globalを作成し、このファイルに.gitignoreと同様に記述していく方法です。 これは.gitconfig.gitignore_globalを読み取るように設定をする必要があります。

git config --global core.excludesfile ~/.gitignore_global

まとめ / 感想

~/.gitignore_globalで設定している人も多いようですが、

  • ホームディレクトリにファイルが置かれる形になること
  • .gitconfigに読み取る設定をしなければならないこと

があるので個人的には~/.config/git/ignoreに書いていく方法が好きです。

【Project Euler】Problem 16 Power digit sumをPythonで解く

この問題をPythonで解いた。

#16 Power digit sum - Project Euler

日本語の問題文はこちら

215 = 32768 であり, 各位の数字の和は 3 + 2 + 7 + 6 + 8 = 26 となる. 同様にして, 21000 の各位の数字の和を求めよ.

Problem 16 - PukiWiki

sum(map(int, str(2**1000)))

【Project Euler】Problem 15 Lattice pathsをPythonで解く

この問題をPythonで解いた。

#15 Lattice paths - Project Euler

日本語の問題文はこちら

2×2 のマス目の左上からスタートした場合, 引き返しなしで右下にいくルートは 6 つある. (画像省略) では, 20×20 のマス目ではいくつのルートがあるか.

Problem 15 - PukiWiki

import math

print(math.factorial(2 * 20) / math.factorial(20) / math.factorial(20))

【Project Euler】Problem 13 Large sumをPythonで解く

この問題をPythonで解いた。

#13 Large sum - Project Euler

日本語の問題文はこちら

以下の50桁の数字100個の合計の上から10桁を求めなさい。

(数字は省略)

Problem 13 - PukiWiki

sample = '''37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676
...(一部省略)
22918802058777319719839450180888072429661980811197
77158542502016545090413245809786882778948721859617
72107838435069186155435662884062257473692284509516
20849603980134001723930671666823555245252804609722
53503534226472524250874054075591789781264330331690'''

numbers = sample.strip().split('\n')
numbers = [int(x) for x in numbers]
solution = sum(numbers)
answer = str(solution)[:10]
answer

【Project Euler】Problem 10 Summation of primesをPythonで解く

この問題をPythonで解いた。

#10 Summation of primes - Project Euler

日本語の問題文はこちら

10以下の素数の和は 2 + 3 + 5 + 7 = 17 である. 200万以下の全ての素数の和を求めよ.

Problem 10 - PukiWiki

from sympy import isprime

result = 0

for num in range(2, 2000000):
    if isprime(num):
        result += num
print(result)

【Project Euler】Problem 9 Special Pythagorean tripletをPythonで解く

この問題をPythonで解いた。

#9 Special Pythagorean triplet - Project Euler

日本語の問題文はこちら

ピタゴラス数(ピタゴラスの定理を満たす自然数)とは a < b < c で以下の式を満たす数の組である.

a2 + b2 = c2 例えば, 32 + 42 = 9 + 16 = 25 = 52 である.

a + b + c = 1000 となるピタゴラスの三つ組が一つだけ存在する. これらの積 abc を計算しなさい.

Problem 9 - PukiWiki

for a in range(1, 1000):
    for b in range(a + 1, 1000):
        c = 1000 - a - b
        if a < b and b < c and c * c == a * a + b * b:
            print(a * b * c)

itertoolモジュールのpermutations関数を使って順列を作る

itertoolモジュールのpermutations関数を使うと、簡単に順列を作ることができる。 permutationはなかなか見慣れない英単語だが、「順列、交換、置換、並べ換え」といった意味がある。パッと思い出せるぐらいには覚えておくと良さそう。

今回はpermutations関数を実行し、使い方や動きを確認する。

前提条件

Google Colab上で確認

このときのバージョンはPython 3.7.12

実行

エンジニアに親しみのあるプログラミング言語についてのランキングを作成する例とした。 permutations関数はiterableオブジェクトを引数に取る。そのため、language変数に3つの言語名を入れたリストを渡している。 これだけで順列を作成できる。

Python

language = ['Python', 'Ruby', 'Go']

import itertools
list(itertools.permutations(language))
[('Python', 'Ruby', 'Go'),
 ('Python', 'Go', 'Ruby'),
 ('Ruby', 'Python', 'Go'),
 ('Ruby', 'Go', 'Python'),
 ('Go', 'Python', 'Ruby'),
 ('Go', 'Ruby', 'Python')]

テーブル形式に変換すると、下記と同じデータになっている。

1位 2位 3位
Python Ruby Go
Python Go Ruby
Ruby Python Go
Ruby Go Python
Go Python Ruby
Go Ruby Python

permutations関数の2つめの引数に、これは取り出したい個数を入力する。今回は2と入力する。 指定することで ['Python', 'Ruby', 'Go']の3つの中から2つだけ取り出したいことを実現することができる。

language = ['Python', 'Ruby', 'Go']

import itertools
list(itertools.permutations(language,2))
[('Python', 'Ruby'),
 ('Python', 'Go'),
 ('Ruby', 'Python'),
 ('Ruby', 'Go'),
 ('Go', 'Python'),
 ('Go', 'Ruby')]
1位 2位
Python Ruby
Python Go
Ruby Python
Ruby Go
Go Python
Go Ruby

ちなみに、元の要素より大きい数を指定すると、空のオブジェクトが返る。 空のリストが表示されているのは、list関数で囲んでいるからである。

language = ['Python', 'Ruby', 'Go']

import itertools
list(itertools.permutations(language,4))
[]

【Project Euler】Problem 7 10001st primeをPythonで解く

この問題をPythonで解いた。

#7 10001st prime - Project Euler

日本語の問題文はこちら

素数を小さい方から6つ並べると 2, 3, 5, 7, 11, 13 であり, 6番目の素数は 13 である. 10 001 番目の素数を求めよ.

Problem 7 - PukiWiki

import sympy

print(sympy.prime(10001))

【Project Euler】Problem 8 Largest product in a seriesをPythonで解く

この問題をPythonで解いた。

#8 Largest product in a series - Project Euler

日本語の問題文はこちら

次の1000桁の数字のうち, 隣接する4つの数字の総乗の中で, 最大となる値は, 9 × 9 × 8 × 9 = 5832である.

73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450 この1000桁の数字から13個の連続する数字を取り出して, それらの総乗を計算する. では、それら総乗のうち、最大となる値はいくらか.

EX 6桁の数123789から5個の連続する数字を取り出す場合, 12378と23789の二通りとなり, 後者の23789=3024が最大の総乗となる.

Problem 8 - PukiWiki

num = '7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450'

from operator import mul
from functools import reduce

ans = []
for i in range(len(num)):
  ans.append(reduce(mul, list(map(int, num[i:i + 13]))) )
print(max(ans))