Find the sum of all the primes below two million.
(defmacro sq (x) `(* ,x ,x)) (defmacro last-item (lst) `(car (last ,lst))) (defun good-reverse (lst) (labels ((rev (lst acc) (if (null lst) acc (rev (cdr lst) (cons (car lst) acc))))) (rev lst nil))) (defun prime-sieve-a-list (input-lst) (labels ((sieve-iter (go-lst acc-list) (if (not go-lst) acc-list (if (> (sq (car go-lst)) (last-item go-lst)) (append (good-reverse acc-list) go-lst) (sieve-iter (remove-if #'(lambda (x) (= (mod x (car go-lst)) 0)) (cdr go-lst)) (cons (car go-lst) acc-list)))))) (sieve-iter input-lst '()))) (defun range (max &key (min 0) (step 1)) (loop :for n :from min :below max :by step collect n)) (defmacro prime-sieve (n) `(prime-sieve-a-list (cons 2 (range (1+ ,n) :min 3 :step 2)))) (defun sum (lst) (reduce #'+ lst)) (sum (prime-sieve 2000000)) (time (sum (prime-sieve 2000000)))
— Me@2023-02-16 11:34:49 PM
.
.
2023.02.21 Tuesday (c) All rights reserved by ACHK
You must be logged in to post a comment.