Cuban primes: Difference between revisions
Content added Content deleted
Line 602: | Line 602: | ||
with multiple columns, commas and all. |
with multiple columns, commas and all. |
||
<lang |
<lang lisp>;;; Show the first 200 and the 100,000th cuban prime. |
||
;;; Cuban primes are the difference of 2 consecutive cubes. |
;;; Cuban primes are the difference of 2 consecutive cubes. |
||
(defun primep (n) |
(defun primep (n) |
||
(cond ((< n 4) t) |
|||
((evenp n) nil) |
|||
((zerop (mod n 3)) nil) |
|||
(t (loop for i from 5 upto (isqrt n) by 6 |
|||
when (or |
|||
(zerop (mod n i)) |
|||
(zerop (mod n (+ i 2)))) |
|||
return nil |
|||
finally (return t))))) |
|||
(defun cube (n) (* n n n)) |
(defun cube (n) (* n n n)) |
||
(defun cuban (n) |
(defun cuban (n) |
||
(loop for i from 1 |
|||
for j from 2 |
|||
for cube-diff = (- (cube j) (cube i)) |
|||
when (primep cube-diff) |
when (primep cube-diff) |
||
collect cube-diff into cuban-primes |
|||
and count i into counter |
|||
when ( |
when (= counter n) |
||
return cuban-primes)) |
|||