34 lines
1006 B
Common Lisp
34 lines
1006 B
Common Lisp
(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)))
|