プロジェクト

全般

プロフィール

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

Noppi, 2023/12/27 01:59

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
(define answer-10
11
  (let ((table (make-vector 2000001 #t))
12
        (count (isqrt 2000000)))
13
    (vector-set! table 0 #f)
14
    (vector-set! table 1 #f)
15
16
    (let loop1 ((index 2))
17
      (cond
18
        [(< count index)
19
         (let loop2 ((index 0) (result 0))
20
           (cond
21
             [(< 2000000 index) result]
22
             [(vector-ref table index)
23
              (loop2 (add1 index) (+ result index))]
24
             [else (loop2 (add1 index) result)]))]
25
        [(vector-ref table index)
26
         (let loop3 ((current (* index 2)))
27
           (cond
28
             [(< 2000000 current)
29
              (loop1 (add1 index))]
30
             [else
31
               (vector-set! table current #f)
32
               (loop3 (+ current index))]))]
33
        [else (loop1 (add1 index))]))))
34
35
(printf "10: ~D~%" answer-10)
36
```