RosettaCodeData/Task/Yellowstone-sequence/Nim/yellowstone-sequence-1.nim

19 lines
460 B
Nim
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
import math
proc yellowstone(n: int): seq[int] =
assert n >= 3
result = @[1, 2, 3]
var present = {1, 2, 3}
var start = 4
while result.len < n:
var candidate = start
while true:
if candidate notin present and gcd(candidate, result[^1]) == 1 and gcd(candidate, result[^2]) != 1:
result.add candidate
present.incl candidate
while start in present: inc start
break
inc candidate
echo yellowstone(30)