Problem 69 » 履歴 » バージョン 3
Noppi, 2024/02/01 01:35
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 | 3 | Noppi | (define (prime-generator) |
47 | (let ([current 11] |
||
48 | [primes '(2 3 5 7)] |
||
49 | [inc 2] |
||
50 | [next-inc (^n (if (= n 2) 4 2))]) |
||
51 | (^[] |
||
52 | (let loop ([rest-primes primes]) |
||
53 | (if (null? rest-primes) |
||
54 | (begin0 |
||
55 | current |
||
56 | (set! primes (append primes `(,current))) |
||
57 | (set! current (+ current inc)) |
||
58 | (set! inc (next-inc inc))) |
||
59 | (let ([p (car rest-primes)]) |
||
60 | (cond |
||
61 | [(< current (* p p)) |
||
62 | (begin0 |
||
63 | current |
||
64 | (set! primes (append primes `(,current))) |
||
65 | (set! current (+ current inc)) |
||
66 | (set! inc (next-inc inc)))] |
||
67 | [(zero? (mod current p)) |
||
68 | (set! current (+ current inc)) |
||
69 | (set! inc (next-inc inc)) |
||
70 | (loop primes)] |
||
71 | [else |
||
72 | (loop (cdr rest-primes))]))))))) |
||
73 | 2 | Noppi | |
74 | 3 | Noppi | (define my-primes |
75 | (generator->lseq 2 3 5 7 (prime-generator))) |
||
76 | 2 | Noppi | |
77 | 3 | Noppi | ;;; n の素因数が p1, p2, ... , pk の時 |
78 | ;;; φ(n) = n * (1 - 1/p1) * (1 - 1/p2) * ... * (1 - 1/pk) |
||
79 | ;;; φ(n)/n = (1 - 1/p1) * (1 - 1/p2) * ... * (1 - 1/pk) |
||
80 | ;;; = (p1 - 1)/p1 * (p2 - 1)/p2 * ... * (pk - 1)/pk |
||
81 | ;;; ∴ n/φ(n) = p1/(p1 - 1) * p2/(p2 - 1) * ... * pk/(pk - 1) |
||
82 | ;;; |
||
83 | ;;; この時の n/φ(n) を最大化することを考えると |
||
84 | ;;; 1. 各 p は小さい方が良い |
||
85 | ;;; 2. 各 p はたくさんあった方が良い |
||
86 | ;;; 以上のことから、小さな素数をかけていって10^6を超えないところ |
||
87 | ;;; までを求めれば良い |
||
88 | 2 | Noppi | |
89 | 3 | Noppi | (define (answer max-num) |
90 | (let loop ([product 1] [rest-primes my-primes]) |
||
91 | (let ([next-product (* product (car rest-primes))]) |
||
92 | (if (< max-num next-product) |
||
93 | product |
||
94 | (loop next-product (cdr rest-primes)))))) |
||
95 | |||
96 | (define answer-69 (answer #e1e6)) |
||
97 | 2 | Noppi | |
98 | (format #t "69: ~d~%" answer-69) |
||
99 | 1 | Noppi | ``` |