We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
What is the largest n-digit pandigital prime that exists?
(defun primep (n)
(cond ((minusp n) 'nil)
((= 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 ConvetToNum(l)
(let ((lengthoflist (length l))
(num 0))
(loop for i from 0 to (- lengthoflist 1)
do(progn (setq num (+ num
(* (elt l i)
(expt 10 (- lengthoflist (+ i 1))))))))
num))
(defun Euler41()
(let ((alllist (p (list 1 2 3 4 5 6 7)))
(currentprime 0)
(tempvar 0))
(loop for i from 0 to 5039
do(progn (setq tempvar (ConvetToNum (elt alllist i)))
(if (primep tempvar)
(setq currentprime tempvar))))
currentprime))
No comments:
Post a Comment