Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,20 @@
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"

View file

@ -0,0 +1,10 @@
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."

View file

@ -0,0 +1,26 @@
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
100_000.times { |i|
var seq = hailstone(i)
if (seq.len > max) {
max = seq.len
n = i
}
}
 
say "Longest sequence is for #{n}: #{max}"
}

View file

@ -0,0 +1 @@
$ sidef Hailstone.sm

View file

@ -0,0 +1,7 @@
include Hailstone
 
var score = Hash()
100_000.times { |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"

View file

@ -0,0 +1 @@
$ sidef test_hailstone.sf