Plaster

text
(defpackage :info.isoraqathedh.happy-numbers (:use :cl) (:nicknames :happy-numbers)) (in-package :happy-numbers) (defun happy-function (number &optional (base 10)) (loop for i = number then (floor (/ i base)) while (plusp i) sum (expt (mod i base) 2))) (defun happy-number-loop (number &optional (base 10)) (assert (and (plusp number) (integerp number)) (number)) (loop with ht = (make-hash-table) for i = number then (happy-function i base) ;; It can be proved mathematically ;; that this function must terminate at some point ;; for any input you can feed it ;; though it's probably unhealthy to let it keep running ;; until it's done. for j from 0 to 999 ; failsafe collect i into happy-number-loop until (gethash i ht) do (setf (gethash i ht) j) finally (return (values happy-number-loop (- j (gethash i ht))))))