読者です 読者をやめる 読者になる 読者になる

mu chance or much chance ?

日々の戯れ言

Project Euler 10 [Python ver.]

Project Euler

問題10を解くRubyプログラムをPythonプログラムに変更しました.

問題は「2000000以下の全ての素数の総和を求めよ」.

http://blog.muchance.jp/entry/2016/08/24/233000
import math

num = 2000000
primeList = []
tempList = []

check = math.sqrt(num)

for i in range(2, num + 1):
    tempList.append(i)

while tempList[0] < check:
    p = tempList.pop(0)
    primeList.append(p)
    tempList = [x for x in tempList if x % p != 0]

primeList += tempList

print(sum(primeList))

エラトステネスの篩を実装しました.

Project Euler 9 [Python ver.]

Project Euler

問題9を解くRubyプログラムをPythonプログラムに変更しました.

問題は「ピタゴラス数(自然数a, b, c,ただし,a < b < c)の合計値が1000における積abcを求めよ」.

http://blog.muchance.jp/entry/2016/08/23/233000
for a in range(1, 1000 - 1):
    for b in range(a + 1, 1000 - a):
        c = 1000 - (a + b)
        if not(a < b and b < c):
            continue
        if c ** 2 == a ** 2 + b ** 2:
            print(a * b * c)

まだ慣れませんね.

Project Euler 8 [Python ver.]

Project Euler

問題8を解くRubyプログラムをPythonプログラムに変更しました.

問題は「1000桁の整数から13個の連続する整数(桁)を取り出して,それらの総乗を計算した場合,総乗の値の最大値を求めよ」.

http://blog.muchance.jp/entry/2016/08/22/233000
numStr = "73167176531330624919225119674426574742355349194934\
96983520312774506326239578318016984801869478851843\
85861560789112949495459501737958331952853208805511\
12540698747158523863050715693290963295227443043557\
66896648950445244523161731856403098711121722383113\
62229893423380308135336276614282806444486645238749\
30358907296290491560440772390713810515859307960866\
70172427121883998797908792274921901699720888093776\
65727333001053367881220235421809751254540594752243\
52584907711670556013604839586446706324415722155397\
53697817977846174064955149290862569321978468622482\
83972241375657056057490261407972968652414535100474\
82166370484403199890008895243450658541227588666881\
16427171479924442928230863465674813919123162824586\
17866458359124566529476545682848912883142607690042\
24219022671055626321111109370544217506941658960408\
07198403850962455444362981230987879927244284909188\
84580156166097919133875499200524063689912560717606\
05886116467109405077541002256983155200055935729725\
71636269561882670428252483600823257530420752963450"

max = 0

for i in range(0, len(numStr) - 12):
    mul = 1
    for j in range(0, 13):
        mul *= int(numStr[i + j])
    if max < mul:
        max = mul

print(max)

forとrangeの使い方にまだ慣れません.

import this

python

pythonを起動して,"import this"と入力すると.

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

と出力されるんですね.

全く知らなかった.