Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
26
Task/Hailstone-sequence/Nim/hailstone-sequence.nim
Normal file
26
Task/Hailstone-sequence/Nim/hailstone-sequence.nim
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
proc hailstone(n: int): seq[int] =
|
||||
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:
|
||||
import strformat, strutils
|
||||
let h = hailstone(27)
|
||||
echo &"Hailstone sequence for number 27 has {h.len} elements."
|
||||
let first = h[0..3].join(", ")
|
||||
let last = h[^4..^1].join(", ")
|
||||
echo &"This sequence begins with {first} and ends with {last}."
|
||||
|
||||
var m, mi = 0
|
||||
for i in 1..<100_000:
|
||||
let n = hailstone(i).len
|
||||
if n > m:
|
||||
m = n
|
||||
mi = i
|
||||
echo &"\nFor numbers < 100_000, maximum length {m} was found for Hailstone({mi})."
|
||||
Loading…
Add table
Add a link
Reference in a new issue