Problem 39 » 履歴 » バージョン 1
Noppi, 2024/01/15 05:18
1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
---|---|---|---|
2 | # [[Problem 39]] |
||
3 | |||
4 | ## Integer Right Triangles |
||
5 | If $p$ is the perimeter of a right angle triangle with integral length sides, $\{a, b, c\}$, there are exactly three solutions for $p = 120$. |
||
6 | |||
7 | $\{20,48,52\}$, $\{24,45,51\}$, $\{30,40,50\}$ |
||
8 | |||
9 | For which value of $p \le 1000$, is the number of solutions maximised? |
||
10 | |||
11 | ## 整数の直角三角形 |
||
12 | 辺の長さが整数の3つ組{a,b,c}である直角三角形を考え, その周囲の長さを p とする. p = 120のときには3つの解が存在する: |
||
13 | |||
14 | {20,48,52}, {24,45,51}, {30,40,50} |
||
15 | |||
16 | p ≤ 1000 のとき解の個数が最大になる p はいくつか? |
||
17 | |||
18 | ```scheme |
||
19 | (import (scheme base) |
||
20 | (gauche base)) |
||
21 | |||
22 | (define triangle-list |
||
23 | (let loop1 ([p 3] [result '()]) |
||
24 | (if (< 1000 p) |
||
25 | result |
||
26 | (let ([a-max (div p 3)]) |
||
27 | (let loop2 ([a 1] |
||
28 | [result result] |
||
29 | [temp-result '()]) |
||
30 | (if (< a-max a) |
||
31 | (loop1 (+ p 1) |
||
32 | (if (null? temp-result) |
||
33 | result |
||
34 | (cons temp-result result))) |
||
35 | (let loop3 ([b a] |
||
36 | [result result] |
||
37 | [temp-result temp-result]) |
||
38 | (let ([c (- p a b)]) |
||
39 | (if (< c b) |
||
40 | (loop2 (+ a 1) result temp-result) |
||
41 | (if (= (+ (* a a) |
||
42 | (* b b)) |
||
43 | (* c c)) |
||
44 | (loop3 (+ b 1) |
||
45 | result |
||
46 | (cons `(,a ,b . ,c) temp-result)) |
||
47 | (loop3 (+ b 1) |
||
48 | result |
||
49 | temp-result))))))))))) |
||
50 | |||
51 | (define count-triangle |
||
52 | (map (^[lis] |
||
53 | (let* ([current (car lis)] |
||
54 | [a (car current)] |
||
55 | [b (cadr current)] |
||
56 | [c (cddr current)]) |
||
57 | `(,(+ a b c) . ,(length lis)))) |
||
58 | triangle-list)) |
||
59 | |||
60 | (define max-count-triangle |
||
61 | (fold (^[c max-lis] |
||
62 | (if (< (cdr max-lis) (cdr c)) |
||
63 | c |
||
64 | max-lis)) |
||
65 | '(0 . 0) |
||
66 | count-triangle)) |
||
67 | |||
68 | (define answer-39 |
||
69 | (car max-count-triangle)) |
||
70 | |||
71 | (format #t "39: ~d~%" answer-39) |
||
72 | ``` |