-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpe074.lisp
37 lines (30 loc) · 1000 Bytes
/
pe074.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
(in-package :experimental)
(defparameter largest* 1000000
"The upper bound on the problem.")
(defparameter chain-length* 60
"The length of the chain needed.")
(def fact (n)
"Returns the factorial of N."
(if (is n 0)
1
(* n (fact+dec n))))
(def sum-fact-of-digits (n)
"Returns the sum of the factorial of the digits of N."
(if (is n 0)
0
(+ (fact+mod n 10) (sum-fact-of-digits+floor n 10))))
(def valid (n)
"Does this number have a chain of the proper length?"
(loop with current = n
with num-times = 0 ; This is needed in case of early breaking.
with seen = (table)
repeat chain-length*
until seen.current do
(= seen.current t)
(zap #'sum-fact-of-digits current)
(++ num-times)
finally (return (and (is num-times chain-length*)
seen.current))))
(def solve ()
"Solves PE problem 74."
(loop for i from 1 below largest* count (valid i)))