Executable library: Difference between revisions
Content added Content deleted
(Nimrod -> Nim) |
(→{{header|Tcl}}: Added zkl) |
||
Line 1,393: | Line 1,393: | ||
puts "most common length is $mostCommonLength, with frequency $freq"</lang> |
puts "most common length is $mostCommonLength, with frequency $freq"</lang> |
||
=={{header|zkl}}== |
|||
zkl is a system of VM, compiler, etc (ala Python) so no need for compiling/linking etc. |
|||
File hailstone.zkl: |
|||
<lang zkl>fcn collatz(n,z=L()){ z.append(n); if(n==1) return(z); |
|||
if(n.isEven) return(self.fcn(n/2,z)); return(self.fcn(n*3+1,z)) } |
|||
h27:=collatz(27); |
|||
println("Hailstone(27)-->",h27[0,4].concat(","),"...", |
|||
h27[-4,*].concat(",")," length ",h27.len()); |
|||
[2..0d100_000].pump(Void, // loop n from 2 to 100,000 |
|||
collatz, // generate Collatz sequence(n) |
|||
fcn(c,n){ // if new longest sequence, save length/C, return longest |
|||
if(c.len()>n[0]) n.clear(c.len(),c[0]); n}.fp1(L(0,0))) |
|||
.println();</lang> |
|||
File hailstone2.zkl: |
|||
<lang zkl>collatz:=Import("hailstone",False,False,False).collatz; // don't run constructor |
|||
d:=Dictionary(); |
|||
[2..0d100_000].pump(Void, // loop n from 2 to 100,000 |
|||
collatz, // generate Collatz sequence(n) |
|||
'wrap(c){ d.incV(c.len()) } // save length |
|||
); |
|||
println("Number of different lengths: ",d.len()); |
|||
longest:=(0).max(d.values); |
|||
mostFreqLen:=d.filter1('wrap([(k,v)]){ v==longest })[0]; |
|||
println("Most frequent length: %d; %d sequences of that length." |
|||
.fmt(mostFreqLen,longest));</lang> |
|||
{{out}} |
|||
<pre> |
|||
$ zkl hailstone |
|||
Hailstone(27)-->27,82,41,124...8,4,2,1 length 112 |
|||
L(351,77031) |
|||
$ zkl hailstone2 |
|||
Number of different lengths: 314 |
|||
Most frequent length: 72; 1467 sequences of that length. |
|||
$ |
|||
</pre> |