Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
20
Task/Executable-library/Nim/executable-library-1.nim
Normal file
20
Task/Executable-library/Nim/executable-library-1.nim
Normal 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"
|
||||
10
Task/Executable-library/Nim/executable-library-2.nim
Normal file
10
Task/Executable-library/Nim/executable-library-2.nim
Normal 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."
|
||||
Loading…
Add table
Add a link
Reference in a new issue