Problem 29 » 履歴 » バージョン 2
Noppi, 2024/01/12 13:30
| 1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
|---|---|---|---|
| 2 | # [[Problem 29]] |
||
| 3 | |||
| 4 | ## Distinct Powers |
||
| 5 | <p>Consider all integer combinations of $a^b$ for $2 \le a \le 5$ and $2 \le b \le 5$:</p> |
||
| 6 | \begin{matrix} |
||
| 7 | 2^2=4, &2^3=8, &2^4=16, &2^5=32\\ |
||
| 8 | 3^2=9, &3^3=27, &3^4=81, &3^5=243\\ |
||
| 9 | 4^2=16, &4^3=64, &4^4=256, &4^5=1024\\ |
||
| 10 | 5^2=25, &5^3=125, &5^4=625, &5^5=3125 |
||
| 11 | \end{matrix} |
||
| 12 | If they are then placed in numerical order, with any repeats removed, we get the following sequence of $15$ distinct terms: |
||
| 13 | $$4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125.$$ |
||
| 14 | How many distinct terms are in the sequence generated by $a^b$ for $2 \le a \le 100$ and $2 \le b \le 100$? |
||
| 15 | |||
| 16 | ## 個別のべき乗 |
||
| 17 | <p>$2 \le a \le 5$ と $2 \le b \le 5$ について, $a^b$ を全て考えてみよう:</p> |
||
| 18 | \begin{matrix} |
||
| 19 | 2^2=4, &2^3=8, &2^4=16, &2^5=32\\ |
||
| 20 | 3^2=9, &3^3=27, &3^4=81, &3^5=243\\ |
||
| 21 | 4^2=16, &4^3=64, &4^4=256, &4^5=1024\\ |
||
| 22 | 5^2=25, &5^3=125, &5^4=625, &5^5=3125 |
||
| 23 | \end{matrix} |
||
| 24 | これらを小さい順に並べ, 同じ数を除いたとすると, 15個の項を得る: |
||
| 25 | $$4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125.$$ |
||
| 26 | $2 \le a \le 100$, $2 \le b \le 100$ で同じことをしたときいくつの異なる項が存在するか? |
||
| 27 | |||
| 28 | ```scheme |
||
| 29 | 2 | Noppi | (import (scheme base) |
| 30 | (gauche base) |
||
| 31 | (scheme sort)) |
||
| 32 | |||
| 33 | (define (unique lis) |
||
| 34 | (let loop ([prev #f] [rest lis] [result '()]) |
||
| 35 | (cond |
||
| 36 | [(null? rest) (reverse result)] |
||
| 37 | [(and prev |
||
| 38 | (= prev (car rest))) |
||
| 39 | (loop prev (cdr rest) result)] |
||
| 40 | [else |
||
| 41 | (loop (car rest) |
||
| 42 | (cdr rest) |
||
| 43 | (cons (car rest) result))]))) |
||
| 44 | |||
| 45 | (define (powers a b) |
||
| 46 | (assume (exact-integer? a)) |
||
| 47 | (assume (exact-integer? b)) |
||
| 48 | (assume (<= 2 a)) |
||
| 49 | (assume (<= 2 b)) |
||
| 50 | (fold (^[a lis] |
||
| 51 | (append (fold (^[b lis] |
||
| 52 | (cons (expt a b) lis)) |
||
| 53 | '() |
||
| 54 | (iota (- b 1) 2)) |
||
| 55 | lis)) |
||
| 56 | '() |
||
| 57 | (iota (- a 1) 2))) |
||
| 58 | |||
| 59 | (define answer-29 |
||
| 60 | (length (unique (list-sort < (powers 100 100))))) |
||
| 61 | |||
| 62 | (format #t "29: ~d~%" answer-29) |
||
| 63 | 1 | Noppi | ``` |