プロジェクト

全般

プロフィール

Problem 44 » 履歴 » バージョン 1

Noppi, 2024/01/16 08:16

1 1 Noppi
[ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]]
2
# [[Problem 44]]
3
4
## Pentagon Numbers
5
Pentagonal numbers are generated by the formula, $P_n=n(3n-1)/2$. The first ten pentagonal numbers are:
6
7
$$1, 5, 12, 22, 35, 51, 70, 92, 117, 145, \dots$$
8
9
It can be seen that $P_4 + P_7 = 22 + 70 = 92 = P_8$. However, their difference, $70 - 22 = 48$, is not pentagonal.
10
11
Find the pair of pentagonal numbers, $P_j$ and $P_k$, for which their sum and difference are pentagonal and $D = |P_k - P_j|$ is minimised; what is the value of $D$?
12
13
## 五角数
14
五角数は $P_n=n(3n-1)/2$ で生成される. 最初の10項は
15
16
$$1, 5, 12, 22, 35, 51, 70, 92, 117, 145, \dots$$
17
18
である.
19
20
$P_4 + P_7 = 22 + 70 = 92 = P_8$ である. しかし差 $70 - 22 = 48$ は五角数ではない.
21
22
五角数のペア $P_j$ と $P_k$ について, 差と和が五角数になるものを考える. 差を $D = |P_k - P_j|$ と書く. 差 $D$ の最小値を求めよ.
23
24
```scheme
25
(import (scheme base)
26
        (gauche base)
27
        (scheme vector))
28
29
(define pentagonal-num
30
  (list->vector
31
    (cdr
32
      (map (^n
33
             (/ (* n
34
                   (- (* n 3) 1))
35
                2))
36
           (iota 10002)))))
37
38
(define (pentagonal-num? n)
39
  (vector-binary-search
40
    pentagonal-num
41
    n
42
    (^[a b] (- a b))))
43
44
(define find-pentagonal-pair
45
  (let loop1 ([k 1])
46
    ; 10000 まで生成した時、7070 + 7071 までは値が範囲を超えなかった
47
    (if (< 7071 k)
48
      (errorf "解が見つかりませんでした~%")
49
      (let ([p_k (vector-ref pentagonal-num k)])
50
        (let loop2 ([j (- k 1)])
51
          (if (negative? j)
52
            (loop1 (+ k 1))
53
            (let ([p_j (vector-ref pentagonal-num j)])
54
              (if (and (pentagonal-num? (- p_k p_j))
55
                       (pentagonal-num? (+ p_k p_j)))
56
                `(,p_j . ,p_k)
57
                (loop2 (- j 1))))))))))
58
59
(define answer-44
60
  (- (cdr find-pentagonal-pair)
61
     (car find-pentagonal-pair)))
62
63
(format #t "44: ~d~%" answer-44)
64
```