Problem 40 » 履歴 » バージョン 2
Noppi, 2024/01/15 07:24
| 1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
|---|---|---|---|
| 2 | # [[Problem 40]] |
||
| 3 | |||
| 4 | ## Champernowne's Constant |
||
| 5 | An irrational decimal fraction is created by concatenating the positive integers: |
||
| 6 | $$0.12345678910{\color{red}\mathbf 1}112131415161718192021\cdots$$ |
||
| 7 | |||
| 8 | It can be seen that the $12$<sup>th</sup> digit of the fractional part is $1$. |
||
| 9 | |||
| 10 | If $d_n$ represents the $n$<sup>th</sup> digit of the fractional part, find the value of the following expression. |
||
| 11 | $$d_1 \times d_{10} \times d_{100} \times d_{1000} \times d_{10000} \times d_{100000} \times d_{1000000}$$ |
||
| 12 | |||
| 13 | ## チャンパーノウン定数 |
||
| 14 | 正の整数を順に連結して得られる以下の10進の無理数を考える: |
||
| 15 | $$0.12345678910{\color{red}\mathbf 1}112131415161718192021\cdots$$ |
||
| 16 | 小数第12位は1である. |
||
| 17 | |||
| 18 | $d_n$ で小数第$n$位の数を表す. $d_1 \times d_{10} \times d_{100} \times d_{1000} \times d_{10000} \times d_{100000} \times d_{1000000}$ を求めよ. |
||
| 19 | |||
| 20 | ```scheme |
||
| 21 | (import (scheme base) |
||
| 22 | (gauche base) |
||
| 23 | (scheme flonum)) |
||
| 24 | |||
| 25 | (define (digit-of-num n) |
||
| 26 | (assume (exact-integer? n)) |
||
| 27 | (assume (<= 0 n)) |
||
| 28 | (if (zero? n) |
||
| 29 | 1 |
||
| 30 | (+ (floor->exact (fllog10 n)) |
||
| 31 | 1))) |
||
| 32 | |||
| 33 | (define (number-list n) |
||
| 34 | (assume (exact-integer? n)) |
||
| 35 | (assume (<= 0 n)) |
||
| 36 | 2 | Noppi | (if (zero? n) |
| 37 | '(0) |
||
| 38 | (let loop ([rest n] |
||
| 39 | [result '()]) |
||
| 40 | (if (<= rest 0) |
||
| 41 | result |
||
| 42 | (loop (div rest 10) |
||
| 43 | (cons (mod rest 10) |
||
| 44 | result)))))) |
||
| 45 | 1 | Noppi | |
| 46 | 2 | Noppi | (define (champernowne d) |
| 47 | (let ([result (make-vector (+ d |
||
| 48 | (digit-of-num d) |
||
| 49 | 1))]) |
||
| 50 | 1 | Noppi | (let loop1 ([current 1] [pos 1]) |
| 51 | 2 | Noppi | (if (< d pos) |
| 52 | 1 | Noppi | result |
| 53 | (let loop2 ([count (digit-of-num current)] |
||
| 54 | [numlis (number-list current)] |
||
| 55 | [pos pos]) |
||
| 56 | (cond |
||
| 57 | [(<= count 0) |
||
| 58 | (loop1 (+ current 1) pos)] |
||
| 59 | [else |
||
| 60 | (vector-set! result pos (car numlis)) |
||
| 61 | (loop2 (- count 1) |
||
| 62 | (cdr numlis) |
||
| 63 | (+ pos 1))])))))) |
||
| 64 | |||
| 65 | (define answer-40 |
||
| 66 | 2 | Noppi | (let ([cn100_0000 (champernowne 1000000)]) |
| 67 | (* (vector-ref cn100_0000 1) |
||
| 68 | (vector-ref cn100_0000 10) |
||
| 69 | (vector-ref cn100_0000 100) |
||
| 70 | (vector-ref cn100_0000 1000) |
||
| 71 | (vector-ref cn100_0000 1_0000) |
||
| 72 | (vector-ref cn100_0000 10_0000) |
||
| 73 | (vector-ref cn100_0000 100_0000)))) |
||
| 74 | 1 | Noppi | |
| 75 | (format #t "40: ~d~%" answer-40) |
||
| 76 | ``` |