プロジェクト

全般

プロフィール

Problem 40 » 履歴 » バージョン 3

Noppi, 2024/01/15 07:45

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