プロジェクト

全般

プロフィール

Problem 40 » 履歴 » リビジョン 2

リビジョン 1 (Noppi, 2024/01/15 06:54) → リビジョン 2/3 (Noppi, 2024/01/15 07:24)

[ホーム](https://redmine.noppi.jp) - [[Wiki|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$<sup>th</sup> digit of the fractional part is $1$. 

 If $d_n$ represents the $n$<sup>th</sup> 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}$ を求めよ. 

 ```scheme 
 (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] 
                
              [count (digit-of-num n)] 
              [result '()]) 
       
     (if (<= rest 0) 
         
       result 
         
       (loop (div rest 10) 
               
             (- count 1) 
             (cons (mod rest 10) 
                     result)))))) result))))) 

 (define (champernowne d) champernowne 
   (let ([result (make-vector (+ d 
                                 (digit-of-num d) 
                                 1))]) 100_0008)]) 
     (let loop1 ([current 1] [pos 1]) 
       (if (< d 100_0000 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 1000000)]) 
     (* (vector-ref cn100_0000 champernowne 1) 
        
      (vector-ref cn100_0000 champernowne 10) 
        
      (vector-ref cn100_0000 champernowne 100) 
        
      (vector-ref cn100_0000 champernowne 1000) 
        
      (vector-ref cn100_0000 champernowne 1_0000) 
        
      (vector-ref cn100_0000 champernowne 10_0000) 
        
      (vector-ref cn100_0000 100_0000)))) champernowne 100_0000))) 

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