Jump to content

Executable library: Difference between revisions

Add Nimrod
(→‎{{header|Limbo}}: Hopefully this should clarify.)
(Add Nimrod)
Line 725:
The length of hailstone sequence that is most common in the range 1 <= N < 100000 is 72. It occurs 1467 times.</pre>
 
=={{header|Nimrod}}==
<lang nimrod>proc hailstone*(n): auto =
result = @[n]
var n = n
while n > 1:
if (n and 1) == 1:
n = 3 * n + 1
else:
n = n div 2
result.add n
 
when isMainModule:
let h = hailstone 27
assert h.len == 112 and h[0..3] == @[27,82,41,124] and h[h.high-3..h.high] == @[8,4,2,1]
var m, mi = 0
for i in 1 .. <100_000:
let n = hailstone(i).len
if n > m:
m = n
mi = i
echo "Maximum length ", m, " was found for hailstone(", mi, ") for numbers <100,000"</lang>
 
In Nimrod the value <code>isMainModule</code> is set at compiletime, and we can use it with <code>when</code> (a compiletime if).
 
'''Library Importing Example'''
<lang nimrod>import hailstone, tables
 
var t = initCountTable[int]()
 
for i in 1 .. <100_000:
t.inc(hailstone(i).len)
 
let (val, cnt) = t.largest()
echo "The length of hailstone sequence that is most common for"
echo "hailstone(n) where 1<=n<100000, is ", val, ". It occurs ", cnt, " times."</lang>
 
'''Example output'''
<pre>The length of hailstone sequence that is most common for
hailstone(n) where 1<=n<100000, is 72. It occurs 1467 times.</pre>
=={{header|Perl}}==
Lib package in file <code>Hailstone.pm</code>:<lang perl>package Hailstone;
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.