Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
15
Task/Happy-numbers/Julia/happy-numbers-1.julia
Normal file
15
Task/Happy-numbers/Julia/happy-numbers-1.julia
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
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) ? break : push!(past,n)
|
||||
end
|
||||
n == 1 && push!(happy_ints,int_try)
|
||||
int_try += 1
|
||||
end
|
||||
return happy_ints
|
||||
end
|
||||
11
Task/Happy-numbers/Julia/happy-numbers-2.julia
Normal file
11
Task/Happy-numbers/Julia/happy-numbers-2.julia
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
sumhappy(n) = sum(x->x^2, digits(n))
|
||||
|
||||
function ishappy(x, mem = [])
|
||||
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) = [z = 1 ; [z = nexthappy(z) for i = 1:n-1]]
|
||||
30
Task/Happy-numbers/Julia/happy-numbers-3.julia
Normal file
30
Task/Happy-numbers/Julia/happy-numbers-3.julia
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
const CACHE = 256
|
||||
buf = zeros(Int,CACHE)
|
||||
buf[1] = 1
|
||||
#happy(n) returns 1 if happy, 0 if not
|
||||
function happy(n)
|
||||
if n < CACHE
|
||||
buf[n] > 0 && return 2-buf[n]
|
||||
buf[n] = 2
|
||||
end
|
||||
sum = 0
|
||||
nn = n
|
||||
while nn != 0
|
||||
x = nn%10
|
||||
sum += x*x
|
||||
nn = int8(nn/10)
|
||||
end
|
||||
x = happy(sum)
|
||||
n < CACHE && (buf[n] = 2-x)
|
||||
return x
|
||||
end
|
||||
function main()
|
||||
i = 1; counter = 1000000
|
||||
while counter > 0
|
||||
if happy(i) == 1
|
||||
counter -= 1
|
||||
end
|
||||
i += 1
|
||||
end
|
||||
return i-1
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue