とんちゃんといっしょ

Cloudに関する技術とか日常とかについて書いたり書かなかったり

Problem 6

問題

What is the difference between the sum of the squares and the square of the sums?

英語

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/index.php?section=problems&id=6
日本語

最初の10個の自然数について、その和の二乗と、二乗数の和は以下の通り。
1² + 2² + ... + 10² = 385
(1 + 2 + ... + 10)² = 3025

これらの数の差は 3025 - 385 = 2640 となる。

同様にして、最初の100個の自然数について和の二乗と二乗の和の差を求めよ。

http://odz.sakura.ne.jp/projecteuler/index.php?Problem%206

解説

1〜100までの総和の2乗と2乗の総和の差を求める問題。
100と書こうとしたけど?dになったのはGolf仕様。

ソースコード(Ruby)

p (1..?d).inject(:+)**2-(1..?d).inject{|s,i|s+i**2}