Problem 41 » 履歴 » バージョン 3
  Noppi, 2024/01/15 12:07 
  
| 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 | 2 | Noppi | (math prime)) | 
| 21 | 1 | Noppi | |
| 22 | ; 考えられる組み合わせとしては | ||
| 23 | ; (1 2 3 4) | ||
| 24 | ; (1 2 3 4 5 6 7) | ||
| 25 | ; のみ | ||
| 26 | ; (他は全て 3 で割り切れる数となる) | ||
| 27 | (define possible-list | ||
| 28 | (map (^[lis] | ||
| 29 | (fold (^[n acc] | ||
| 30 | (+ (* acc 10) | ||
| 31 | n)) | ||
| 32 | 0 | ||
| 33 | lis)) | ||
| 34 | (append (permutations (iota 4 1)) | ||
| 35 | (permutations (iota 7 1))))) | ||
| 36 | |||
| 37 | (define prime-pandigital-list | ||
| 38 | 3 | Noppi | (filter small-prime? | 
| 39 | 1 | Noppi | possible-list)) | 
| 40 | |||
| 41 | (define answer-41 | ||
| 42 | (apply max prime-pandigital-list)) | ||
| 43 | |||
| 44 | (format #t "41: ~d~%" answer-41) | ||
| 45 | ``` |