Problem 69 » 履歴 » バージョン 2
Noppi, 2024/01/30 12:39
| 1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
|---|---|---|---|
| 2 | # [[Problem 69]] |
||
| 3 | |||
| 4 | ## Totient Maximum |
||
| 5 | Euler's totient function, $\phi(n)$ [sometimes called the phi function], is defined as the number of positive integers not exceeding $n$ which are relatively prime to $n$. For example, as $1$, $2$, $4$, $5$, $7$, and $8$, are all less than or equal to nine and relatively prime to nine, $\phi(9)=6$. |
||
| 6 | |||
| 7 | | **$n$** | **Relatively Prime** | **$\phi(n)$** | **$n/\phi(n)$** | |
||
| 8 | |--|--|--|--| |
||
| 9 | | 2 | 1 | 1 | 2 | |
||
| 10 | | 3 | 1,2 | 2 | 1.5 | |
||
| 11 | | 4 | 1,3 | 2 | 2 | |
||
| 12 | | 5 | 1,2,3,4 | 4 | 1.25 | |
||
| 13 | | 6 | 1,5 | 2 | 3 | |
||
| 14 | | 7 | 1,2,3,4,5,6 | 6 | 1.1666... | |
||
| 15 | | 8 | 1,3,5,7 | 4 | 2 | |
||
| 16 | | 9 | 1,2,4,5,7,8 | 6 | 1.5 | |
||
| 17 | | 10 | 1,3,7,9 | 4 | 2.5 | |
||
| 18 | |||
| 19 | It can be seen that $n = 6$ produces a maximum $n/\phi(n)$ for $n\leq 10$. |
||
| 20 | |||
| 21 | Find the value of $n\leq 1\,000\,000$ for which $n/\phi(n)$ is a maximum. |
||
| 22 | |||
| 23 | ## トーティエント関数の最大値 |
||
| 24 | オイラーのトーティエント関数, φ(n) [時々ファイ関数とも呼ばれる]は, n と互いに素な n 未満の数の数を定める. たとえば, 1, 2, 4, 5, 7, そして8はみな9未満で9と互いに素であり, φ(9)=6. |
||
| 25 | |||
| 26 | | n | 互いに素な数 | φ(n) | n/φ(n) | |
||
| 27 | |--|--|--|--| |
||
| 28 | | 2 | 1 | 1 | 2 | |
||
| 29 | | 3 | 1,2 | 2 | 1.5 | |
||
| 30 | | 4 | 1,3 | 2 | 2 | |
||
| 31 | | 5 | 1,2,3,4 | 4 | 1.25 | |
||
| 32 | | 6 | 1,5 | 2 | 3 | |
||
| 33 | | 7 | 1,2,3,4,5,6 | 6 | 1.1666... | |
||
| 34 | | 8 | 1,3,5,7 | 4 | 2 | |
||
| 35 | | 9 | 1,2,4,5,7,8 | 6 | 1.5 | |
||
| 36 | | 10 | 1,3,7,9 | 4 | 2.5 | |
||
| 37 | |||
| 38 | n ≤ 10 では n/φ(n) の最大値は n=6 であることがわかる. |
||
| 39 | |||
| 40 | n ≤ 1,000,000で n/φ(n) が最大となる値を見つけよ. |
||
| 41 | |||
| 42 | ```scheme |
||
| 43 | 2 | Noppi | (import (scheme base) |
| 44 | (gauche base)) |
||
| 45 | |||
| 46 | (define (factorize num) |
||
| 47 | (assume (exact-integer? num)) |
||
| 48 | (assume (<= 2 num)) |
||
| 49 | (let loop ([current 2] [rest num] [result '()]) |
||
| 50 | (cond |
||
| 51 | [(= rest 1) (reverse result)] |
||
| 52 | [(and (< num (* current current)) |
||
| 53 | (null? result)) |
||
| 54 | '()] |
||
| 55 | [(< num (* current current)) |
||
| 56 | (reverse (cons rest result))] |
||
| 57 | [(and (zero? (mod rest current)) |
||
| 58 | (null? result)) |
||
| 59 | (loop current |
||
| 60 | (div rest current) |
||
| 61 | (cons current result))] |
||
| 62 | [(and (zero? (mod rest current)) |
||
| 63 | (= current (car result))) |
||
| 64 | (loop current |
||
| 65 | (div rest current) |
||
| 66 | result)] |
||
| 67 | [(zero? (mod rest current)) |
||
| 68 | (loop current |
||
| 69 | (div rest current) |
||
| 70 | (cons current result))] |
||
| 71 | [else |
||
| 72 | (loop (+ current 1) rest result)]))) |
||
| 73 | |||
| 74 | ; https://manabitimes.jp/math/667 |
||
| 75 | (define (totient num) |
||
| 76 | (assume (exact-integer? num)) |
||
| 77 | (assume (<= 2 num)) |
||
| 78 | (let ([lis (factorize num)]) |
||
| 79 | (if (null? lis) |
||
| 80 | (- num 1) |
||
| 81 | (apply * num |
||
| 82 | (map (^n (- 1 (/ 1 n))) |
||
| 83 | lis))))) |
||
| 84 | |||
| 85 | (define (totient-100_0000-list) |
||
| 86 | (map (^n |
||
| 87 | (let ([totient-n (totient n)]) |
||
| 88 | `(,n ,totient-n ,(/ n totient-n)))) |
||
| 89 | (iota (- #e1e6 1) 2))) |
||
| 90 | |||
| 91 | (define answer-69 |
||
| 92 | (car |
||
| 93 | (fold (^[lis current] |
||
| 94 | (if (< (caddr current) (caddr lis)) |
||
| 95 | lis |
||
| 96 | current)) |
||
| 97 | '(0 0 0) |
||
| 98 | (totient-100_0000-list)))) |
||
| 99 | |||
| 100 | (format #t "69: ~d~%" answer-69) |
||
| 101 | 1 | Noppi | ``` |