(defun pow-mod (a b m) (let ((r 1)) (loop while (> b 0) when (oddp b) do (setq r (mod (* r a) m)) do (setq a (mod (* a a) m)) (setq b (ash b -1))) r)) (defun miller-rabin (n bases) (let ((d (1- n)) (s 0)) (loop while (evenp d) do (incf s) (setq d (ash d -1))) (dolist (base bases) (if (<= n base) (return-from miller-rabin t) (let ((a (pow-mod base d n))) (unless (= a 1) (let ((r 1)) (loop while (/= a (1- n)) if (= r s) do (return-from miller-rabin nil) else do (incf r) (setq a (mod (* a a) n)))))))) t)) (defun primep (n) (cond ((<= n 1) nil) ((<= n 3) t) ((evenp n) nil) ((< n 4759123141) (miller-rabin n '(2 7 61))) (t (miller-rabin n '(2 325 9375 28178 450775 9780504 1795265022))))) (defun main (&rest argv) (declare (ignorable argv)) (let* ((n (read))) (dotimes (_ n) (let* ((x (read))) (format t "~d ~d~%" x (if (primep x) 1 0)))))) (main)