Problem 41 » 履歴 » バージョン 1
Noppi, 2024/01/15 08:30
| 1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
|---|---|---|---|
| 2 | # [[Problem 41]] |
||
| 3 | |||
| 4 | ## Pandigital Prime |
||
| 5 | We shall say that an $n$-digit number is pandigital if it makes use of all the digits $1$ to $n$ exactly once. For example, $2143$ is a $4$-digit pandigital and is also prime. |
||
| 6 | |||
| 7 | What is the largest $n$-digit pandigital prime that exists? |
||
| 8 | |||
| 9 | ## パンデジタル素数 |
||
| 10 | n桁パンデジタルであるとは, 1からnまでの数を各桁に1つずつ持つこととする. |
||
| 11 | |||
| 12 | #下のリンク先にあるような数学的定義とは異なる |
||
| 13 | |||
| 14 | 例えば2143は4桁[パンデジタル数](https://ja.wikipedia.org/wiki/%E3%83%91%E3%83%B3%E3%83%87%E3%82%B8%E3%82%BF%E3%83%AB%E6%95%B0)であり, かつ素数である. n桁(この問題の定義では9桁以下)パンデジタルな素数の中で最大の数を答えよ. |
||
| 15 | |||
| 16 | ```scheme |
||
| 17 | (import (scheme base) |
||
| 18 | (gauche base) |
||
| 19 | (util combinations) |
||
| 20 | (math prime) |
||
| 21 | (scheme list)) |
||
| 22 | |||
| 23 | (define temp-primes (primes)) |
||
| 24 | |||
| 25 | (define (prime? num) |
||
| 26 | (= (car |
||
| 27 | (drop-while (cut < <> num) |
||
| 28 | temp-primes)) |
||
| 29 | num)) |
||
| 30 | |||
| 31 | ; 考えられる組み合わせとしては |
||
| 32 | ; (1 2 3 4) |
||
| 33 | ; (1 2 3 4 5 6 7) |
||
| 34 | ; のみ |
||
| 35 | ; (他は全て 3 で割り切れる数となる) |
||
| 36 | (define possible-list |
||
| 37 | (map (^[lis] |
||
| 38 | (fold (^[n acc] |
||
| 39 | (+ (* acc 10) |
||
| 40 | n)) |
||
| 41 | 0 |
||
| 42 | lis)) |
||
| 43 | (append (permutations (iota 4 1)) |
||
| 44 | (permutations (iota 7 1))))) |
||
| 45 | |||
| 46 | (define prime-pandigital-list |
||
| 47 | (filter prime? |
||
| 48 | possible-list)) |
||
| 49 | |||
| 50 | (define answer-41 |
||
| 51 | (apply max prime-pandigital-list)) |
||
| 52 | |||
| 53 | (format #t "41: ~d~%" answer-41) |
||
| 54 | ``` |