fork download
  1. (defparameter *memoization* (make-hash-table))
  2.  
  3. (defun digits (n &optional (l (list)))
  4. (if (= n 0)
  5. l
  6. (multiple-value-bind (a b) (floor n 10)
  7. (digits a (cons b l)))))
  8.  
  9. (defun square-digits (n &aux (hash (gethash n *memoization*)))
  10. (if hash
  11. hash
  12. (loop for i in (digits n) summing (expt i 2))))
  13.  
  14. (defun process (n)
  15. (loop
  16. for c = (square-digits n)
  17. until (or (= c 1) (= c 89))
  18. collect c into results
  19. do (setf n c)
  20. finally
  21. (loop for i in results do (setf (gethash i *memoization*) c))
  22. (return c)))
  23.  
  24. (loop
  25. with result = 0
  26. for i from 1 to 10000000
  27. do
  28. (if (= (process i) 89) (incf result))
  29. finally (print result))
  30.  
  31. ;(loop for i being the hash-keys in *memoization* using (hash-value j)
  32. ; do (format t "~a ~a ~%" i j))
  33.  
  34.  
Success #stdin #stdout 3.11s 203840KB
stdin
Standard input is empty
stdout
8581146