Problem 24 » 履歴 » バージョン 2
Noppi, 2024/01/06 07:57
| 1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
|---|---|---|---|
| 2 | # [[Problem 24]] |
||
| 3 | |||
| 4 | ## Lexicographic Permutations |
||
| 5 | A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are: |
||
| 6 | |||
| 7 | <div style="text-align:center">012 021 102 120 201 210</div> |
||
| 8 | |||
| 9 | What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? |
||
| 10 | |||
| 11 | ## 辞書式順列 |
||
| 12 | 順列とはモノの順番付きの並びのことである. たとえば, 3124は数 1, 2, 3, 4 の一つの順列である. すべての順列を数の大小でまたは辞書式に並べたものを辞書順と呼ぶ. 0と1と2の順列を辞書順に並べると |
||
| 13 | |||
| 14 | <div style="text-align:center">012 021 102 120 201 210</div> |
||
| 15 | |||
| 16 | になる. |
||
| 17 | |||
| 18 | 0,1,2,3,4,5,6,7,8,9からなる順列を辞書式に並べたときの100万番目はいくつか? |
||
| 19 | |||
| 20 | ```scheme |
||
| 21 | 2 | Noppi | (import (scheme base) |
| 22 | (gauche base)) |
||
| 23 | |||
| 24 | (define (my-permutation lis) |
||
| 25 | (let perm-sub ([rest lis] [sub '()] [result '()]) |
||
| 26 | (if (null? rest) |
||
| 27 | (cons (reverse sub) result) |
||
| 28 | (fold-right (^[n result] |
||
| 29 | (perm-sub (delete n rest) (cons n sub) result)) |
||
| 30 | result |
||
| 31 | rest)))) |
||
| 32 | |||
| 33 | (define answer-24 |
||
| 34 | (list-ref (my-permutation (iota 10)) 99_9999)) |
||
| 35 | |||
| 36 | (format #t "24: ~d~%" answer-24) |
||
| 37 | 1 | Noppi | ``` |