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 Common Lisp>;;; Show the first 200 and the 100,000th cuban prime.
<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)
(cond ((< n 4) t)
((evenp n) nil)
((evenp n) nil)
((zerop (mod n 3)) nil)
((zerop (mod n 3)) nil)
(t (loop for i from 5 upto (isqrt n) by 6
(t (loop for i from 5 upto (isqrt n) by 6
when (or
when (or
(zerop (mod n i))
(zerop (mod n i))
(zerop (mod n (+ i 2))))
(zerop (mod n (+ i 2))))
return nil
return nil
finally (return t)))))
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
(loop for i from 1
for j from 2
for j from 2
for cube-diff = (- (cube j) (cube i))
for cube-diff = (- (cube j) (cube i))
when (primep cube-diff)
when (primep cube-diff)
collect cube-diff into cuban-primes
collect cube-diff into cuban-primes
and count i into counter
and count i into counter
when (equal counter n)
when (= counter n)
return cuban-primes))
return cuban-primes))