プロジェクト

全般

プロフィール

Problem 26 » 履歴 » バージョン 2

Noppi, 2024/01/10 13:23

1 1 Noppi
[ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]]
2
# [[Problem 26]]
3
4
## Reciprocal Cycles
5
A unit fraction contains $1$ in the numerator. The decimal representation of the unit fractions with denominators $2$ to $10$ are given:
6
7
$1/2 = 0.5$
8
$1/3 =0.(3)$
9
$1/4 =0.25$
10
$1/5 = 0.2$
11
$1/6 = 0.1(6)$
12
$1/7 = 0.(142857)$
13
$1/8 = 0.125$
14
$1/9 = 0.(1)$
15
$1/10 = 0.1$
16
17
Where $0.1(6)$ means $0.166666\cdots$, and has a $1$-digit recurring cycle. It can be seen that $1/7$ has a $6$-digit recurring cycle.
18
19
Find the value of $d \lt 1000$ for which $1/d$ contains the longest recurring cycle in its decimal fraction part.
20
21
## 逆数の循環節 その1
22
単位分数とは分子が1の分数である. 分母が2から10の単位分数を10進数で表記すると次のようになる.
23
24
1/2 = 0.5
25
1/3 = 0.(3)
26
1/4 = 0.25
27
1/5 = 0.2
28
1/6 = 0.1(6)
29
1/7 = 0.(142857)
30
1/8 = 0.125
31
1/9 = 0.(1)
32
1/10 = 0.1
33
34
0.1(6)は 0.166666... という数字であり, 1桁の循環節を持つ. 1/7 の循環節は6桁ある.
35
36
d < 1000 なる 1/d の中で小数部の循環節が最も長くなるような d を求めよ.
37
38
```scheme
39 2 Noppi
(import (scheme base)
40
        (gauche base)
41
        (scheme sort))
42
43
(define (get-recurring-cycle n)
44
  (let loop ([index 0] [current-mod 1] [lis '()])
45
    (cond
46
      [(zero? current-mod) 0]
47
      [(find (^c (= current-mod (cdr c))) lis)
48
       => (^c (- index (car c)))]
49
      [else (loop (+ index 1)
50
                  (mod (* current-mod 10) n)
51
                  (cons `(,index . ,current-mod) lis))])))
52
53
(define (enum-recurring-cycles n)
54
  (map (^n (cons n (get-recurring-cycle n)))
55
       (iota (- n 1) 1)))
56
57
(define answer-26
58
  (car
59
    (last
60
      (list-sort (^[a b] (< (cdr a) (cdr b)))
61
                 (enum-recurring-cycles 1000)))))
62
63
(format #t "26: ~d~%" answer-26)
64 1 Noppi
```