summaryrefslogtreecommitdiff
path: root/problem-10.scm
blob: d9848313f15e2fabc49d71382747269a37412d07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(use-modules (srfi srfi-1))

(define (divisible? m n)
  (zero? (modulo m n)))

(define (prime-sieve n)
  (define (mark-multiples! bits x)
    (define (mark y)
      (when (< y n)
        (bitvector-set! bits (1- y) #t)
        (mark (+ y x))))
    (mark x))
  (define (sieve m primes bits)
    (cond ((= m n)
           primes)
          ((bitvector-ref bits (1- m))
           (sieve (1+ m) primes bits))
          (else
           (mark-multiples! bits m)
           (sieve (1+ m) (cons m primes) bits))))
  (sieve 2 '() (make-bitvector n)))

(reduce + 0 (prime-sieve 2000000))