
(defparameter *primes-up-to-1000* (let ((sieve (make-array 1001 :element-type 'bit :initial-element 0))) (loop for i from 2 to 1000 when (zerop (sbit sieve i)) do (loop for j from (* i i) to 1000 by i do (setf (sbit sieve j) 1))) (loop for i from 2 to 1000 when (zerop (sbit sieve i)) collect i))) (defun primep (n) (cond ((<= n 1) nil) ((<= n 1000) (member n *primes-up-to-1000*)) (t (loop for p in *primes-up-to-1000* while (<= (* p p) n) never (zerop (mod n p)))))) (defun prime-sequence-length (a b) (loop for n from 0 while (primep (+ (* n n) (* a n) b)) count t)) (defun find-best-quadratic-coefficients (&optional (max-a 1000) (max-b 1000)) (loop with best-length = 0 with best-a = 0 with best-b = 0 for a from (- max-a) to max-a do (loop for b in *primes-up-to-1000* when (> b max-b) do (loop-finish) do (let ((len (prime-sequence-length a b))) (when (> len best-length) (setf best-length len best-a a best-b b)))) finally (return (list best-length best-a best-b)))) (defun euler-27 () (destructuring-bind (len a b) (find-best-quadratic-coefficients) (format t "Sequence length: ~d, a: ~d, b: ~d~%" len a b) (* a b))) (defun main () (time (format t "Answer: ~d~%" (euler-27))))
; SLIME 2.28 CL-USER> (main) Sequence length: 71, a: -61, b: 971 Answer: -59231 Evaluation took: 0.103 seconds of real time 0.103747 seconds of total run time (0.103637 user, 0.000110 system) 100.97% CPU 258,919,169 processor cycles 0 bytes consed NIL CL-USER>
— Me@2025-02-23 03:48:23 PM
.
.
2025.02.23 Sunday (c) All rights reserved by ACHK






You must be logged in to post a comment.