RosettaCodeData/Task/Yellowstone-sequence/Fennel/yellowstone-sequence.fennel
2025-08-11 18:05:26 -07:00

45 lines
1.3 KiB
Fennel

(do ;;; Yellowstone sequence - translation of the Pluto sample
(fn gcd [m n]
(var (a b) (values (math.abs m) (math.abs n)))
(while (not= b 0)
(set (b a) (values (% a b) b))
)
a
)
(fn yellowstone [n]
(var (a m) (values [1 2 3] [true true true]))
(for [x 4 n]
(tset a x 0)
(tset m x false)
)
(var minV 4)
(for [c 4 n]
(var (more i) (values true minV))
(while more
(when (and (not (. m i))
(= (gcd (. a (- c 1)) i) 1)
(> (gcd (. a (- c 2)) i) 1)
)
(tset a c i)
(tset m i true)
(when (= i minV)
(set minV (+ 1 minV))
)
(set more false)
)
(set i (+ i 1))
)
)
a
)
(local (ySize perLine) (values 30 10))
(local y (yellowstone ySize))
(io.write (string.format "The first %d Yellowstone numbers are:\n" ySize))
(for [yPos 1 ySize]
(io.write (string.format " %2d" (. y yPos)))
(when (= 0 (% yPos perLine)) (print))
)
)