Problem 40 » 履歴 » バージョン 1
Noppi, 2024/01/15 06:54
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 | (let loop ([rest n] |
||
37 | [count (digit-of-num n)] |
||
38 | [result '()]) |
||
39 | (if (<= rest 0) |
||
40 | result |
||
41 | (loop (div rest 10) |
||
42 | (- count 1) |
||
43 | (cons (mod rest 10) result))))) |
||
44 | |||
45 | (define champernowne |
||
46 | (let ([result (make-vector 100_0008)]) |
||
47 | (let loop1 ([current 1] [pos 1]) |
||
48 | (if (< 100_0000 pos) |
||
49 | result |
||
50 | (let loop2 ([count (digit-of-num current)] |
||
51 | [numlis (number-list current)] |
||
52 | [pos pos]) |
||
53 | (cond |
||
54 | [(<= count 0) |
||
55 | (loop1 (+ current 1) pos)] |
||
56 | [else |
||
57 | (vector-set! result pos (car numlis)) |
||
58 | (loop2 (- count 1) |
||
59 | (cdr numlis) |
||
60 | (+ pos 1))])))))) |
||
61 | |||
62 | (define answer-40 |
||
63 | (* (vector-ref champernowne 1) |
||
64 | (vector-ref champernowne 10) |
||
65 | (vector-ref champernowne 100) |
||
66 | (vector-ref champernowne 1000) |
||
67 | (vector-ref champernowne 1_0000) |
||
68 | (vector-ref champernowne 10_0000) |
||
69 | (vector-ref champernowne 100_0000))) |
||
70 | |||
71 | (format #t "40: ~d~%" answer-40) |
||
72 | ``` |