操作
Problem 10 » 履歴 » リビジョン 2
« 前 |
リビジョン 2/8
(差分)
| 次 »
Noppi, 2023/12/27 02:26
Problem 10¶
#!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)]
[(vector-ref table index)
(let loop2 ((current (* index 2)))
(cond
[(< 2000000 current)
(loop1 (add1 index))]
[else
(vector-set! table current #f)
(loop2 (+ current index))]))]
[else (loop1 (add1 index))]))))
(printf "10: ~D~%" answer-10)
Noppi が2023/12/27に更新 · 2件の履歴