# b5be17536fc6

`Clean up, comment`
author Steve Losh Wed, 04 Oct 2017 00:51:13 -0400 9cdfe55bc3f1 9cbd4c08480e (none) src/problems.lisp src/utils.lisp

## Changes

```--- a/src/problems.lisp	Tue Oct 03 22:13:02 2017 -0400
+++ b/src/problems.lisp	Wed Oct 04 00:51:13 2017 -0400
@@ -2071,7 +2071,7 @@
;; A "strong, right-truncatable Harshad prime" is not itself
-             ;; a Harshad number.  Prime numbers can never be Harshad numbers
+             ;; a Harshad number!  Prime numbers can never be Harshad numbers
;; (except for single-digit numbers), because otherwise they would
;; have to have a divisor (the sum of their digits) (thanks
;; lebossle).
@@ -2083,17 +2083,22 @@
(truncate-number-right number 1))))
-             (sum
-               (recursively ((digits ()))
-                 (let ((n (digits-to-number digits :from-end t)))
-                   (cond
-                     ((or (>= n limit)) nil)
-                     ((or (zerop n) (harshadp n))
-                      (mapcan #'recur
-                              (loop :for i :from (if (null digits) 1 0) :to 9
-                                    :collect (cons i digits))))
-                     (t nil)))))))
+             ;; Instead of trying to check every number below 10^14 for
+             ;; primality, we can recursively build right-truncatable Harshad
+             ;; numbers until we hit a non-Harshad (or the limit), then test
+             ;;
+             ;; If we work from the left (starting at the high order digits)
+             ;; and make sure the number is a Harshad number at each step,
+             ;; we automatically ensure it's right truncatable.
+             (recursively ((n 0))
+               (cond
+                 ((>= n limit) 0)
+                 ((or (zerop n) (harshadp n))
+                  (iterate (for digit :from (if (zerop n) 1 0) :to 9)
+                           (summing (recur (append-digit digit n)))))
+                 (t 0)))))

```
```--- a/src/utils.lisp	Tue Oct 03 22:13:02 2017 -0400
+++ b/src/utils.lisp	Wed Oct 04 00:51:13 2017 -0400
@@ -75,6 +75,11 @@
(collect d :at :beginning)))

+(defun-inline append-digit (digit number &optional (radix 10))
+  "Appened `digit` to `number` in base `radix` as a low-order digit."
+  (+ digit (* number radix)))
+
+
(defun-inline nth-digit (n integer &optional (radix 10))
"Return the `n`th digit of `integer` in base `radix`, counting from the right."