It doesn’t matter

Euler problem 3.1

.

The bad news is: You cannot make people like, love, understand, validate, accept, or be nice to you.

The good news is: It doesn’t matter.

(defmacro sq (x)
  `(* ,x ,x))

(defmacro list-head (lst)
  `(car ,lst))

(defmacro list-tail (lst)
  `(cdr ,lst))

(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 (list-head go-lst))
            (last-item go-lst))

             (append (good-reverse acc-list)
                 go-lst)
             
             (sieve-iter
              (remove-if #'(lambda (x)
                     (=
                      (mod x (list-head go-lst))
                      0))
                 (list-tail go-lst))
              (cons (list-head 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))))

(time (length (prime-sieve 1234567)))

;; 0.764 seconds of real time
;; 95360

(time (length (prime-sieve 12345678)))

;; 20.128 seconds of real time
;; 809227

— Me@2022-08-27 07:59:30 PM

.

.

2022.08.28 Sunday (c) All rights reserved by ACHK