Assistance in understanding the implementation of the Eratosthenes Decision

It's boring, I know, but I need a little help in understanding the implementation of Sieve Eratosthenes. This solution is this Praxis programming problem .

(define (primes n)
  (let* ((max-index (quotient (- n 3) 2))
         (v (make-vector (+ 1 max-index) #t)))
    (let loop ((i 0) (ps '(2)))
      (let ((p (+ i i 3)) (startj (+ (* 2 i i) (* 6 i) 3)))
        (cond ((>= (* p p) n)
               (let loop ((j i) (ps ps))
                  (cond ((> j max-index) (reverse ps))
                        ((vector-ref v j)
                          (loop (+ j 1) (cons (+ j j 3) ps)))
                        (else (loop (+ j 1) ps)))))
              ((vector-ref v i)
                (let loop ((j startj))
                  (if (<= j max-index)
                      (begin (vector-set! v j #f)
                             (loop (+ j p)))))
                      (loop (+ 1 i) (cons p ps)))
              (else (loop (+ 1 i) ps)))))))

I have a problem with startj. Now I see that it pwill cycle through odd numbers, starting with 3, defined as (+ i i 3). But I do not understand the relationship between pand startjwhich (+ (* 2 i i) (* 6 i) 3).


Change . I understand that the idea is to skip previously sifted numbers. The definition of the puzzle states that when sifting a number, the xsifting should start with a square x. So, when sifting 3, start by removing 9, etc.

, startj ().

:

n n-, n .

. Theres a 2 , , - . Theres a 3 , , 0, 1 2 arent . , 6 2 3, , , , .


- , . !

+5
2

, programpraxis .

, startj (+ (* 2 i i) (* 6 i) 3), .. 2i^2 + 6i + 3.

, , p, "sifting" p p^2, , startj - 4i^2 + 12i + 9.

startj v, , 3. p^2 (p^2 - 3) / 2.

: (p^2 - 3) / 2= ([4i^2 + 12i + 9] - 3) / 2= 2i^2 + 6i + 3 - startj.

, startj (quotient (- (* p p) 3) 2), - , :)

+4

: , , , .

: . .

: .

. Praxis.

EDIT. " Praxis". , 6 ; , .

+3

All Articles