F# で Project Euler Problem 6
最初の10個の自然数について, その二乗の和は,
12 + 22 + ... + 102 = 385
最初の10個の自然数について, その和の二乗は,
(1 + 2 + ... + 10)2 = 3025
これらの数の差は 3025 - 385 = 2640 となる.
同様にして, 最初の100個の自然数について二乗の和と和の二乗の差を求めよ.
12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2 = 552 = 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.
単純な加算
> let i = - [1..10] - |> List.map(fun x -> x * x) - |> List.sum - ;; val i : int = 385 > let j = - [1..10] - |> List.sum - ;; val j : int = 55 > let j = j * j - ;; val j : int = 3025 > j - i - ;; val it : int = 2640
> let i = - [1..100] - |> List.map(fun x -> x * x) - |> List.sum - ;; val i : int = 338350 > let j = - [1..100] - |> List.sum - ;; val j : int = 5050 > let j = j * j - ;; val j : int = 25502500 > j - i - ;; val it : int = 25164150 >
数列の和の公式
> let i = 10 * (10 + 1) / 2 - ;; val i : int = 55 > let i = i * i - ;; val i : int = 3025 > let j = 10 * (10 + 1) * (2 * 10 + 1) / 6 - ;; val j : int = 385 > i - j - ;; val it : int = 2640
> let i = 100 * (100 + 1) / 2 - ;; val i : int = 5050 > let i = i * i - ;; val i : int = 25502500 > let j = 100 * (100 + 1) * (2 * 100 + 1) / 6 - ;; val j : int = 338350 > i - j - ;; val it : int = 25164150 >