プロジェクト

全般

プロフィール

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

Noppi, 2024/01/13 13:56

1 1 Noppi
[ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]]
2
# [[Problem 33]]
3
4
## Digit Cancelling Fractions
5
The fraction $49/98$ is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that $49/98 = 4/8$, which is correct, is obtained by cancelling the $9$s.
6
7
We shall consider fractions like, $30/50 = 3/5$, to be trivial examples.
8
9
There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.
10
11
If the product of these four fractions is given in its lowest common terms, find the value of the denominator.
12
13
## 桁消去分数
14
49/98 は面白い分数である.「分子と分母からそれぞれ9を取り除くと, 49/98 = 4/8 となり, 簡単な形にすることができる」と経験の浅い数学者が誤って思い込んでしまうかもしれないからである. (方法は正しくないが,49/98 の場合にはたまたま正しい約分になってしまう.)
15
16
我々は 30/50 = 3/5 のようなタイプは自明な例だとする.
17
18
このような分数のうち, 1より小さく分子・分母がともに2桁の数になるような "自明でない" ものは, 4個ある.
19
20
その4個の分数の積が約分された形で与えられたとき, 分母の値を答えよ.
21
22
```scheme
23 2 Noppi
(import (scheme base)
24
        (gauche base))
25
26
; a > b, a != 0, b != 0, c != 0
27
; 10b + c
28
; -------
29
; 10a + c
30
(define cancelling-frac-1
31
  (fold (^[a lis]
32
          (fold (^[b lis]
33
                  (fold (^[c lis]
34
                          (let ([numer (+ (* 10 b) c)]
35
                                [denom (+ (* 10 a) c)])
36
                            (if (= (/ numer denom) (/ b a))
37
                              (cons `(,numer . ,denom) lis)
38
                              lis)))
39
                        lis
40
                        (iota 9 1)))
41
                lis
42
                (iota (- a 1) 1)))
43
        '()
44
        (iota 8 2)))
45
46
; a > b, c != 0
47
; 10c + b
48
; -------
49
; 10c + a
50
(define cancelling-frac-2
51
  (fold (^[a lis]
52
          (fold (^[b lis]
53
                  (fold (^[c lis]
54
                          (let ([numer (+ (* 10 c) b)]
55
                                [denom (+ (* 10 c) a)])
56
                            (if (= (/ numer denom) (/ b a))
57
                              (cons `(,numer . ,denom) lis)
58
                              lis)))
59
                        lis
60
                        (iota 9 1)))
61
                lis
62
                (iota (- a 1))))
63
        '()
64
        (iota 9 1)))
65
66
; c >= b, a != 0, b != 0, c != 0
67
; 10b + c
68
; -------
69
; 10c + a
70
(define cancelling-frac-3
71
  (fold (^[c lis]
72
          (fold (^[b lis]
73
                  (fold (^[a lis]
74
                          (let ([numer (+ (* 10 b) c)]
75
                                [denom (+ (* 10 c) a)])
76
                            (if (and (< numer denom)
77
                                     (= (/ numer denom) (/ b a)))
78
                              (cons `(,numer . ,denom) lis)
79
                              lis)))
80
                        lis
81
                        (iota 9 1)))
82
                lis
83
                (iota c 1)))
84
        '()
85
        (iota 9 1)))
86
87
; a >= c, a != 0, c!= 0
88
; 10c + b
89
; -------
90
; 10a + c
91
(define cancelling-frac-4
92
  (fold (^[a lis]
93
          (fold (^[c lis]
94
                  (fold (^[b lis]
95
                          (let ([numer (+ (* 10 c) b)]
96
                                [denom (+ (* 10 a) c)])
97
                            (if (and (< numer denom)
98
                                     (= (/ numer denom) (/ b a)))
99
                              (cons `(,numer . ,denom) lis)
100
                              lis)))
101
                        lis
102
                        (iota 10)))
103
                lis
104
                (iota a 1)))
105
        '()
106
        (iota 9 1)))
107
108
(define cancelling-frac
109
  (append cancelling-frac-1
110
          cancelling-frac-2
111
          cancelling-frac-3
112
          cancelling-frac-4))
113
114
(define answer-33
115
  (denominator (fold (^[r result]
116
                       (* result
117
                          (/ (car r) (cdr r))))
118
                     1
119
                     cancelling-frac)))
120
121
(format #t "33: ~d~%" answer-33)
122 1 Noppi
```