The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.
There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.
How many circular primes are there below one million?
My Solution
(defun primep (n)
(cond ((= 2 n) t)
((evenp n) 'nil)
((= 1 n) 'nil)
(t (let ((limit (floor (sqrt n))))
(do ((i 3 (incf i 2)))
((> i limit) t)
(if (= 0 (mod n i)) (return-from primep 'nil)))))))
(defun IsCircular(n)
(let* ((numberstr (format nil "~a" n))
(lengthofstr (length numberstr))
(IsCircularnumber T))
(loop for i from 0 to (- lengthofstr 1)
do (let ((tempstr (concatenate 'string
(subseq numberstr i lengthofstr)
(subseq numberstr 0 i))))
(if (null (primep (parse-integer tempstr)))
(progn (setq IsCircularnumber nil)(return IsCircularnumber)))))
IsCircularnumber))
(defun Euler35(n)
(loop for i from 2 to n
when (IsCircular i)
sum 1))
No comments:
Post a Comment