2015/10/28

エラトステネスの篩

再帰

(define (eratosthenes n)
  (define (recur lis)
    (if (null? lis)
        '()
        (cons (car lis) (recur
                       (filter
                        (lambda (x) (not (= (modulo x (car lis)) 0)))
                        lis)))))
  (recur (iota (- (round n) 1) 2)))

繰り返しで

(define (eratosthenes n)
  (define (iter lis result)
    (if (null? lis)
        result
        (iter (filter (lambda (x) (not (= (modulo x (car lis)) 0)))
                      lis)
              (append result (list (car lis))))))
   (iter (iota (- n 1) 2) '()))

できるだけ元の論理通りにやってみました.
あの斜線つけるやつね.


© 2022 wat-aro