何かを書き留める何か

数学や読んだ本について書く何かです。最近は社会人として生き残りの術を学ぶ日々です。

Project Euler Problem 6

The sum of the squares of the first ten natural numbers is,
1^2+2^2+...+10^2 = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)^2 = 55^2 = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

http://projecteuler.net/problem=6

 

1から100の和の二乗と二乗の和の差を求める問題。

そのまま実装すればよい。

----

print pow(sum([x for x in xrange(1,101)]),2) - sum([pow(x,2) for x in xrange(1,101)])

----

一般には和の二乗と二乗の和のどちらが大きいかはわからないが、自然数とわかっているので和の二乗のほうが大きくなる。

(a+b+c)^2 = a^2+b^2+c^2 +2ab + 2bc + 2ca

から分かる。