RosettaCodeData/Task/First-class-environments/Phix/first-class-environments-1....

49 lines
984 B
Plaintext

function hail(integer n)
if remainder(n,2)=0 then
n /= 2
else
n = 3*n+1
end if
return n
end function
sequence hails = tagset(12),
counts = repeat(0,12),
results = columnize({hails})
function step(integer edx)
integer n = hails[edx]
if n=1 then return 0 end if
n = hail(n)
hails[edx] = n
counts[edx] += 1
results[edx] &= n
return 1
end function
procedure main()
bool done = false
while not done do
done = true
for i=1 to 12 do
if step(i) then
done = false
end if
end for
end while
for i=1 to max(counts)+1 do
for j=1 to 12 do
puts(1,iff(i<=length(results[j])?sprintf("%4d",{results[j][i]}):" "))
end for
puts(1,"\n")
end for
printf(1," %s\n",{join(repeat("===",12))})
for j=1 to 12 do
printf(1,"%4d",{counts[j]})
end for
puts(1,"\n")
end procedure
main()