Problem 9 » 履歴 » バージョン 3
Noppi, 2023/12/27 13:30
1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
---|---|---|---|
2 | # [[Problem 9]] |
||
3 | |||
4 | 3 | Noppi | ## Special Pythagorean Triplet |
5 | A Pythagorean triplet is a set of three natural numbers, $a \lt b \lt c$, for which, |
||
6 | $$a^2 + b^2 = c^2.$$ |
||
7 | For example, $3^2 + 4^2 = 9 + 16 = 25 = 5^2$. |
||
8 | There exists exactly one Pythagorean triplet for which $a + b + c = 1000$. |
||
9 | Find the product $abc$. |
||
10 | |||
11 | ## 特別なピタゴラス数 |
||
12 | ピタゴラス数(ピタゴラスの定理を満たす自然数)とは a < b < c で以下の式を満たす数の組である. |
||
13 | $$a^2 + b^2 = c^2.$$ |
||
14 | 例えば, $3^2 + 4^2 = 9 + 16 = 25 = 5^2$ である. |
||
15 | a + b + c = 1000 となるピタゴラスの三つ組が一つだけ存在する. |
||
16 | これらの積 abc を計算しなさい. |
||
17 | |||
18 | 1 | Noppi | ```scheme |
19 | #!r6rs |
||
20 | #!chezscheme |
||
21 | |||
22 | (import (chezscheme)) |
||
23 | |||
24 | (define answer-9 |
||
25 | 2 | Noppi | (let loop1 ([a 1]) |
26 | 1 | Noppi | (if (< 333 a) |
27 | (error "answer-9" "answer-9 was not found!") |
||
28 | 2 | Noppi | (let loop2 ([b a]) |
29 | 1 | Noppi | (if (< 499 b) |
30 | (loop1 (add1 a)) |
||
31 | 2 | Noppi | (let ([c (- 1000 a b)]) |
32 | (if (= (+ (expt a 2) |
||
33 | (expt b 2)) |
||
34 | (expt c 2)) |
||
35 | 1 | Noppi | (* a b c) |
36 | (loop2 (add1 b))))))))) |
||
37 | |||
38 | (printf "9: ~D~%" answer-9) |
||
39 | ``` |