Happy numbers: Difference between revisions
Content added Content deleted
imported>Maxima enthusiast No edit summary |
|||
Line 4,574: | Line 4,574: | ||
{{out}} |
{{out}} |
||
<pre>1 7 10 13 19 23 28 31 </pre> |
<pre>1 7 10 13 19 23 28 31 </pre> |
||
=={{header|Maxima}}== |
|||
<syntaxhighlight lang="maxima"> |
|||
/* 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); |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[1,7,10,13,19,23,28,31] |
|||
</pre> |
|||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |