プロジェクト

全般

プロフィール

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

リビジョン 1 (Noppi, 2023/12/27 01:59) → リビジョン 2/8 (Noppi, 2023/12/27 02:26)

[ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] 
 # [[Problem 10]] 

 ```scheme 
 #!r6rs 
 #!chezscheme 

 (import (chezscheme)) 

 (define (sum-prime prime?-table) 
   (let* ((len (vector-length prime?-table)) 
          (index-list (iota len)) 
          (prime?-list (vector->list prime?-table))) 
     (fold-left 
       (lambda (sum index prime?) 
         (if prime? 
           (+ sum index) 
           sum)) 
       0 
       index-list 
       prime?-list))) 

 (define answer-10 
   (let ((table (make-vector 2000001 #t)) 
         (count (isqrt 2000000))) 
     (vector-set! table 0 #f) 
     (vector-set! table 1 #f) 

     (let loop1 ((index 2)) 
       (cond 
         [(< count index) (sum-prime table)] 
          (let loop2 ((index 0) (result 0)) 
            (cond 
              [(< 2000000 index) result] 
              [(vector-ref table index) 
               (loop2 (add1 index) (+ result index))] 
              [else (loop2 (add1 index) result)]))] 
         [(vector-ref table index) 
          (let loop2 loop3 ((current (* index 2))) 
            (cond 
              [(< 2000000 current) 
               (loop1 (add1 index))] 
              [else 
                (vector-set! table current #f) 
                (loop2 (loop3 (+ current index))]))] 
         [else (loop1 (add1 index))])))) 

 (printf "10: ~D~%" answer-10) 
 ```