RosettaCodeData/Task/Executable-library/Sidef/executable-library-1.sidef

27 lines
572 B
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

func hailstone(n) {
gather {
while (n > 1) {
take(n)
n = (n.is_even ? (n/2) : (take(3*n + 1)/2))
}
take(1)
}
}
if (__FILE__ == __MAIN__) { # true when not imported
var seq = hailstone(27)
say "hailstone(27) - #{seq.len} elements: #{seq.first(4)} [...] #{seq.last(4)}"
var n = 0
var max = 0
100_000.times { |i|
var seq = hailstone(i)
if (seq.len > max) {
max = seq.len
n = i
}
}
say "Longest sequence is for #{n}: #{max}"
}