Happy numbers: Difference between revisions
m
→{{header|Uiua}}: slightly nicer algorithm
m (→{{header|Uiua}}: slightly nicer algorithm) |
|||
(10 intermediate revisions by 8 users not shown) | |||
Line 15:
Display an example of your output here on this page.
;Related tasks:
* [[Iterated digits squaring]]
;See also:
Line 180 ⟶ 183:
</pre>
=={{header|ABC}}==
<syntaxhighlight lang="ABC">HOW TO RETURN square.digit.sum n:
PUT 0 IN sum
WHILE n>0:
PUT n mod 10 IN digit
PUT sum + digit ** 2 IN sum
PUT floor (n/10) IN n
RETURN sum
HOW TO REPORT happy n:
PUT {} IN seen
WHILE n not.in seen:
INSERT n IN seen
PUT square.digit.sum n IN n
REPORT n=1
HOW TO RETURN next.happy n:
PUT n+1 IN n
WHILE NOT happy n: PUT n+1 IN n
RETURN n
PUT 0 IN n
FOR i IN {1..8}:
PUT next.happy n IN n
WRITE n/</syntaxhighlight>
{{out}}
<Pre>1
7
10
13
19
23
28
31</pre>
=={{header|ACL2}}==
<syntaxhighlight lang="lisp">(include-book "arithmetic-3/top" :dir :system)
Line 2,806 ⟶ 2,843:
if ((count += 1) >= 8) { break }
}</syntaxhighlight>
=={{header|EasyLang}}==
<syntaxhighlight>
func dsum n .
while n > 0
d = n mod 10
s += d * d
n = n div 10
.
return s
.
func happy n .
while n > 999
n = dsum n
.
len seen[] 999
repeat
n = dsum n
until seen[n] = 1
seen[n] = 1
.
return if n = 1
.
while cnt < 8
n += 1
if happy n = 1
cnt += 1
write n & " "
.
.
</syntaxhighlight>
{{out}}
<pre>
1 7 10 13 19 23 28 31
</pre>
=={{header|Eiffel}}==
Line 2,884 ⟶ 2,956:
=={{header|Elena}}==
{{trans|C#}}
ELENA
<syntaxhighlight lang="elena">import extensions;
import system'collections;
Line 2,896 ⟶ 2,968:
while (num != 1)
{
if (cache.indexOfElement
{
^ false
Line 2,903 ⟶ 2,975:
while (num != 0)
{
int digit := num.mod
sum += (digit*digit);
num /= 10
Line 3,934 ⟶ 4,006:
<syntaxhighlight lang="julia">
function happy(x)
happy_ints =
int_try = 1
while length(happy_ints) < x
n = int_try
past =
while n != 1
push!(past, n)
end
n == 1 && push!(happy_ints,int_try)
int_try += 1
Line 3,962 ⟶ 4,035:
<syntaxhighlight lang="julia">sumhappy(n) = sum(x->x^2, digits(n))
function ishappy(x, mem = Int[])
x == 1 ? true :
x in mem ? false :
ishappy(sumhappy(x), [mem ; x])
end
nexthappy
happy(n) = accumulate((a, b) -> nexthappy(a), 1:n)
</syntaxhighlight>
{{Out}}
Line 3,980 ⟶ 4,052:
{{trans|C}}
<syntaxhighlight lang="julia">const CACHE = 256
buf = zeros(Int, CACHE)
buf[
function happy(n)
if n < CACHE
Line 3,988 ⟶ 4,060:
buf[n] = 2
end
nn = n
while nn != 0
nn, x = divrem(nn
end
x = happy(
n < CACHE && (buf[n] = 2 - x)
return x
end
function main()
i, counter = 1
while counter > 0
if happy(i)
counter -= 1
end
i += 1
end
return i - 1
end
</syntaxhighlight>
=={{header|K}}==
Line 4,574 ⟶ 4,647:
{{out}}
<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}}==
Line 6,324 ⟶ 6,430:
There's more than one way to do it...
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <ShowFirst 8 Happy 1>;
};
ShowFirst {
0 s.F s.I = ;
s.N s.F s.I, <Mu s.F s.I>: T =
<Prout s.I>
<ShowFirst <- s.N 1> s.F <+ s.I 1>>;
s.N s.F s.I =
<ShowFirst s.N s.F <+ s.I 1>>;
};
Happy {
1 e.X = T;
s.N e.X s.N e.Y = F;
s.N e.X = <Happy <SqDigSum s.N> s.N e.X>;
};
SqDigSum {
0 = 0;
s.N, <Symb s.N>: s.Ds e.Rs,
<Numb s.Ds>: s.D,
<Numb e.Rs>: s.R =
<+ <* s.D s.D> <SqDigSum s.R>>;
};</syntaxhighlight>
{{out}}
<pre>1
7
10
13
19
23
28
31</pre>
=={{header|Relation}}==
Line 6,372 ⟶ 6,515:
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/*REXX program computes and displays a specified range of happy numbers. */
Call time 'R'
linesize=80
Parse Arg low high /* obtain range of happy numbers */
If low='?' Then Call help
If low='' Then low=10
If high='' Then
Parse Value 1 low With low high
Do i=0 To 9 /*build a squared decimal digit table. */
square.i=i*i
End
happy.=0 /* happy.m=1 - m is a happy number */
unhappy.=0 /* unhappy.n=1 - n is an unhappy number*/
hapn=0 /* count of the happy numbers */
ol=''
Do n=1 While hapn<high /* test integers starting with 1 */
If unhappy.n Then /* if n is unhappy, */
Iterate /* then try next number */
work=n
suml='' /* list of computed sums */
Do Forever
sum=0
Do length(work) /* compute sum of squared digits */
Parse Var work digit +1 work
sum=sum+square.digit
End
Select
When unhappy.sum |, /* sum is known to be unhappy */
wordpos(sum,suml)>0 Then Do /* or was already encountered */
-- If wordpos(sum,suml)>0 Then say 'Loop' n':' suml sum
-- If n<7 Then say n':' suml sum
unhappy.n=1 /* n is unhappy */
Call set suml /* amd so are all sums so far */
Iterate n
End
When sum=1 Then Do /* we reached sum=1 */
hapn+=1 /* increment number of happy numbers */
happy.n=1 /* n is happy */
If hapn>=low Then /* if it is in specified range */
Call out n /* output it */
If hapn=high Then /* end of range reached */
Leave n /* we are done */
Iterate n /* otherwise proceed */
End
Otherwise Do /* otherwise */
suml=suml sum /* add sum to list of sums */
work=sum /* proceed with the new sum */
End
End
End
End
If ol>'' Then /* more output data */
Say strip(ol) /* write to console */
-- Say time('E')
Exit
set:
Parse Arg list
Do While list<>''
Parse Var list s list
unhappy.s=1
End
Return
out:
Parse Arg hn
If length(ol hn)>linesize Then Do /* if it does not fit */
End
Else /* otherwise
ol=ol hn /* append is to the output line */
Return
help:
Say 'rexx hno low high show happy numbers from index low to high'
Exit
</syntaxhighlight>
{{out}}
<pre>
K:\_B\HN>rexx hno ?
rexx hno n compute and show the first n happy numbers
rexx hno low high show happy numbers from index low to
K:\_B\HN>rexx hno 8
1 7 10 13 19 23 28 31
K:\_B\HN>rexx hno 1000 1003
6899 6904 6917 6923
</pre>
=={{header|Ring}}==
Line 7,240 ⟶ 7,286:
31 is a happy number
</pre>
=={{header|Uiua}}==
{{works with|Uiua|0.10.0-dev.1}}
<syntaxhighlight lang="Uiua">
HC ← /+ⁿ2≡⋕°⋕ # Happiness calc = sum of squares of digits
IH ← |2 memo⟨IH ⊙⊂.|=1⟩∊,, HC # Apply HC until seen value recurs
Happy ← ⟨0◌|∘⟩IH : [1] . # Pre-load `seen` with 1. Return start number or 0
# Brute force approach isn't too bad with memoisation even for high bounds.
↙8⊚>0≡Happy⇡10000
# But iterative approach is still much faster
NH ← |1 ⟨NH|∘⟩≠0Happy.+1 # Find next Happy number
⇌[⍥(NH.) 7 1]
</syntaxhighlight>
=={{header|UNIX Shell}}==
Line 7,384 ⟶ 7,445:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="
var m = {}
while (n > 1) {
|