SHARE
TWEET

Untitled

a guest Aug 12th, 2017 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defun digits (n) (map 'vector #'digit-char-p (princ-to-string n)))
  2. (defun num (digits)
  3.   (let ((num 0)
  4.         (wdigits (reverse digits)))
  5.     (loop for digit across digits do
  6.          (incf num digit)
  7.          (setf num (* num 10)))
  8.     (/ num 10)))
  9.  
  10. (defun prime-p (num)
  11.   (if (< num 3)
  12.       (equal num 2)
  13.       (not
  14.        (loop for i from 2 to (floor (sqrt num)) do
  15.             (when (zerop (mod num i))
  16.               (return t))))))
  17.  
  18. (defun rtrunc (number)
  19.   (labels ((work (dgts)
  20.              (when (prime-p (num dgts))
  21.                (if (equal 1 (length dgts))
  22.                    t
  23.                    (rtrnc (subseq dgts 0 (1- (length dgts))))))))
  24.     (work (digits number))))
  25.  
  26. (defmacro ltrunc (number)
  27.   (labels ((work (dgts)
  28.                (when (prime-p (num dgts))
  29.                  (if (equal 1 (length dgts))
  30.                      t
  31.                      (ltrnc (subseq dgts 1))))))
  32.     (work (digits number))))
  33.  
  34.  
  35. (defun problem-37 ()
  36.   (let ((count 0)
  37.         (current 8)
  38.         (nums nil))
  39.     (loop while (< count 11) do
  40.          (when (and (ltrunc current) (rtrunc current))
  41.            (print current)
  42.            (push current nums)
  43.            (incf count))
  44.          (incf current))
  45.     (reduce #'+ nums)))
RAW Paste Data
Top