Happy numbers: Difference between revisions

m
→‎{{header|Julia}}: update for Julia versions > 0.7
(Added Easylang)
m (→‎{{header|Julia}}: update for Julia versions > 0.7)
Line 3,972:
<syntaxhighlight lang="julia">
function happy(x)
happy_ints = ref(Int)[]
int_try = 1
while length(happy_ints) < x
n = int_try
past = ref(Int)[]
while n != 1
n = sum([y^2 for y in digits(n)])
contains(past,n) ?in breakpast :&& push!(past,n)break
push!(past, n)
end
n == 1 && push!(happy_ints,int_try)
int_try += 1
Line 4,000 ⟶ 4,001:
<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 (x) = ishappy(x+1) ? x+1 : nexthappy(x+1)
happy(n) = [zaccumulate((a, =b) 1 ; [z =-> nexthappy(za) for i =, 1:n-1]])
 
happy(n) = [z = 1 ; [z = nexthappy(z) for i = 1:n-1]]
</syntaxhighlight>
{{Out}}
Line 4,018:
{{trans|C}}
<syntaxhighlight lang="julia">const CACHE = 256
buf = zeros(Int, CACHE)
buf[1begin] = 1
 
#happy(n) returns 1 if happy, 0 if not
function happy(n)
if n < CACHE
Line 4,026:
buf[n] = 2
end
sumsqsum = 0
nn = n
while nn != 0
nn, x = divrem(nn%, 10)
sumsqsum += x * x
nn = int8(nn/10)
end
x = happy(sumsqsum)
n < CACHE && (buf[n] = 2 - x)
return x
end
 
function main()
i, counter = 1; counter =, 1000000
while counter > 0
if happy(i) =!= 10
counter -= 1
end
i += 1
end
return i - 1
end
end</syntaxhighlight>
 
=={{header|K}}==
4,105

edits