プロジェクト

全般

プロフィール

Problem 10 » 履歴 » バージョン 2

Noppi, 2023/12/27 02:26

1 1 Noppi
[ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]]
2
# [[Problem 10]]
3
4
```scheme
5
#!r6rs
6
#!chezscheme
7
8
(import (chezscheme))
9
10 2 Noppi
(define (sum-prime prime?-table)
11
  (let* ((len (vector-length prime?-table))
12
         (index-list (iota len))
13
         (prime?-list (vector->list prime?-table)))
14
    (fold-left
15
      (lambda (sum index prime?)
16
        (if prime?
17
          (+ sum index)
18
          sum))
19
      0
20
      index-list
21
      prime?-list)))
22
23 1 Noppi
(define answer-10
24
  (let ((table (make-vector 2000001 #t))
25
        (count (isqrt 2000000)))
26
    (vector-set! table 0 #f)
27
    (vector-set! table 1 #f)
28
29
    (let loop1 ((index 2))
30
      (cond
31 2 Noppi
        [(< count index) (sum-prime table)]
32 1 Noppi
        [(vector-ref table index)
33 2 Noppi
         (let loop2 ((current (* index 2)))
34 1 Noppi
           (cond
35
             [(< 2000000 current)
36
              (loop1 (add1 index))]
37
             [else
38
               (vector-set! table current #f)
39 2 Noppi
               (loop2 (+ current index))]))]
40 1 Noppi
        [else (loop1 (add1 index))]))))
41
42
(printf "10: ~D~%" answer-10)
43
```