Another lazy solution

This commit is contained in:
scms 2024-03-07 10:32:18 -08:00
parent 10230400ea
commit 322a1ecfe3
1 changed files with 33 additions and 0 deletions

33
p87.lisp Normal file
View File

@ -0,0 +1,33 @@
(defun primep (n)
(loop for i from 2 upto (sqrt n)
do (when (= (mod n i) 0)
(return nil))
finally (return t)))
(defun make-prime-enumerator ()
(let ((max 1))
(lambda ()
(loop for n upfrom (1+ max)
until (primep n)
finally (setf max n)
(return n)))))
(defun prime-power-triples (&optional (below 50000000))
(let ((solutions (make-hash-table)))
(loop with next-prime-a = (make-prime-enumerator)
for a = (funcall next-prime-a)
for square = (* a a)
until (>= square below)
do (loop with next-prime-b = (make-prime-enumerator)
for b = (funcall next-prime-b)
for cube = (* b b b)
for sum-b = (+ square cube)
until (>= sum-b below)
do (loop with next-prime-c = (make-prime-enumerator)
for c = (funcall next-prime-c)
for fourth = (* c c c c)
for sum = (+ square cube fourth)
until (>= sum below)
do (setf (gethash sum solutions) t))))
(loop for h being the hash-keys of solutions
sum 1)))