Executable library: Difference between revisions

m
Added Sidef
m (→‎{{header|Perl 6}}: typo fixes)
m (Added Sidef)
Line 1,366:
 
C:\Users\FransAdm\Documents></pre>
 
=={{header|Sidef}}==
Library saved as '''Hailstone.sm'''
<lang ruby>func hailstone(n) {
gather {
while (n > 1) {
take(n);
n = (n.is_even ? n/2 : (3*n + 1));
}
take(1)
}
}
 
if (__FILE__ == __MAIN__) { # true when not imported
var seq = hailstone(27);
say "hailstone(27) - #{seq.len} elements: #{seq.ft(0, 3)} [...] #{seq.ft(-4)}";
 
var n = 0;
var max = 0;
1...100_000 -> each { |i|
var seq = hailstone(i);
if (seq.len > max) {
max = seq.len;
n = i;
}
}
 
say "Longest sequence is for #{n}: #{max}";
}</lang>
 
It can be run with:
<lang shell>$ sidef Hailstone.pm</lang>
 
It can then be used with a program such as:
<lang ruby>include Hailstone;
 
var score = Hash();
1...100_000 -> each { |i| score{ Hailstone::hailstone(i).len } := 0 ++ }
 
var k = score.keys.max_by {|k| score{k} };
say "Most common length is #{k}, occurring #{score{k}} times";</lang>
 
Called with a command line as:
<lang shell>$ sidef test_hailstone.sf</lang>
 
The library is searched in the directories specified in the '''SIDEF_INC''' environment variable, which defaults to the current directory.
 
=={{header|Tcl}}==
2,747

edits