25 lines
770 B
Text
25 lines
770 B
Text
/* Function that decomposes te number into a list */
|
|
decompose(N) := block(
|
|
digits: [],
|
|
while N > 0 do
|
|
(remainder: mod(N, 10),
|
|
digits: cons(remainder, digits),
|
|
N: floor(N/10)),
|
|
digits
|
|
)$
|
|
|
|
/* Function that given a number returns the sum of their digits */
|
|
sum_squares_digits(n):=block(
|
|
decompose(n),
|
|
map(lambda([x],x^2),%%),
|
|
apply("+",%%))$
|
|
|
|
/* Predicate function based on the task iterated digits squaring */
|
|
happyp(n):=if n=1 then true else if n=89 then false else block(iter:n,while not member(iter,[1,89]) do iter:sum_squares_digits(iter),iter,if iter=1 then true)$
|
|
|
|
/* Test case */
|
|
/* First eight happy numbers */
|
|
block(
|
|
happy:[],i:1,
|
|
while length(happy)<8 do (if happyp(i) then happy:endcons(i,happy),i:i+1),
|
|
happy);
|