総和の上位10桁

30分プログラム、その276。総和の上位10桁 via Project Euler

以下の50桁の数字100個の総和の上位10桁を求めよ。

37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
...

これはたぶんn桁同士の和は高々n+1桁だから・・・とかやるんだろうなぁ、と思いつつPythonで素直に計算してみたら、あっさり解けた。なんか拍子抜け。

使い方

$ cat numbers.txt
37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676
89261670696623633820136378418383684178734361726757
....

$ time python problem13.py numbers.txt
5537376230
python problem13.py numbers.txt  0.03s user 0.16s system 72% cpu 0.269 total

ソースコード

def read(filename):
  return map(lambda x: int(x),open(filename))

print str(sum(read('numbers.txt')))[0:10]