Problem 6 » 履歴 » バージョン 4
Noppi, 2023/12/29 04:18
1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
---|---|---|---|
2 | # [[Problem 6]] |
||
3 | |||
4 | 3 | Noppi | ## Sum Square Difference |
5 | The sum of the squares of the first ten natural numbers is, |
||
6 | $$1^2 + 2^2 + ... + 10^2 = 385.$$ |
||
7 | The square of the sum of the first ten natural numbers is, |
||
8 | $$(1 + 2 + ... + 10)^2 = 55^2 = 3025.$$ |
||
9 | 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$. |
||
10 | Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. |
||
11 | |||
12 | ## 二乗和の差 |
||
13 | 最初の10個の自然数について, その二乗の和は, |
||
14 | $$1^2 + 2^2 + ... + 10^2 = 385.$$ |
||
15 | 最初の10個の自然数について, その和の二乗は, |
||
16 | $$(1 + 2 + ... + 10)^2 = 55^2 = 3025.$$ |
||
17 | これらの数の差は 3025 - 385 = 2640 となる. |
||
18 | 同様にして, 最初の100個の自然数について二乗の和と和の二乗の差を求めよ. |
||
19 | |||
20 | 1 | Noppi | ```scheme |
21 | 2 | Noppi | #!r6rs |
22 | 1 | Noppi | #!chezscheme |
23 | 2 | Noppi | |
24 | (import (chezscheme)) |
||
25 | |||
26 | 4 | Noppi | (define square-nums |
27 | 3 | Noppi | (map |
28 | 4 | Noppi | (lambda (n) (expt n 2)) |
29 | 3 | Noppi | (iota 101))) |
30 | |||
31 | (define square-sum |
||
32 | 4 | Noppi | (apply + square-nums)) |
33 | 3 | Noppi | |
34 | (define sum-square |
||
35 | (expt (apply + (iota 101)) |
||
36 | 2)) |
||
37 | |||
38 | 2 | Noppi | (define answer-6 |
39 | 3 | Noppi | (- sum-square square-sum)) |
40 | 1 | Noppi | |
41 | (printf "6: ~D~%" answer-6) |
||
42 | ``` |