プロジェクト

全般

プロフィール

操作

ホーム - Project Euler

Problem 40

Champernowne's Constant

An irrational decimal fraction is created by concatenating the positive integers:

$$0.12345678910{\color{red}\mathbf 1}112131415161718192021\cdots$$

It can be seen that the $12$th digit of the fractional part is $1$.

If $d_n$ represents the $n$th digit of the fractional part, find the value of the following expression.

$$d_1 \times d_{10} \times d_{100} \times d_{1000} \times d_{10000} \times d_{100000} \times d_{1000000}$$

チャンパーノウン定数

正の整数を順に連結して得られる以下の10進の無理数を考える:

$$0.12345678910{\color{red}\mathbf 1}112131415161718192021\cdots$$

小数第12位は1である.

$d_n$ で小数第$n$位の数を表す. $d_1 \times d_{10} \times d_{100} \times d_{1000} \times d_{10000} \times d_{100000} \times d_{1000000}$ を求めよ.

(import (scheme base)
        (gauche base)
        (scheme flonum))

(define (digit-of-num n)
  (assume (exact-integer? n))
  (assume (<= 0 n))
  (if (zero? n)
    1
    (+ (floor->exact (fllog10 n))
       1)))

(define (number-list n)
  (assume (exact-integer? n))
  (assume (<= 0 n))
  (if (zero? n)
    '(0)
    (let loop ([rest n]
               [result '()])
      (if (<= rest 0)
        result
        (loop (div rest 10)
              (cons (mod rest 10)
                    result))))))

(define (champernowne d)
  (let ([result (make-vector (+ d
                                (digit-of-num d)
                                1))])
    (let loop1 ([current 1] [pos 1])
      (if (< d pos)
        result
        (let loop2 ([count (digit-of-num current)]
                    [numlis (number-list current)]
                    [pos pos])
          (cond
            [(<= count 0)
             (loop1 (+ current 1) pos)]
            [else
              (vector-set! result pos (car numlis))
              (loop2 (- count 1)
                     (cdr numlis)
                     (+ pos 1))]))))))

(define answer-40
  (let ([cn100_0000 (champernowne 100_0000)])
    (* (vector-ref cn100_0000 1)
       (vector-ref cn100_0000 10)
       (vector-ref cn100_0000 100)
       (vector-ref cn100_0000 1000)
       (vector-ref cn100_0000 1_0000)
       (vector-ref cn100_0000 10_0000)
       (vector-ref cn100_0000 100_0000))))

(format #t "40: ~d~%" answer-40)

Noppi2024/01/15に更新 · 3件の履歴