RosettaCodeData/Task/Euclid-Mullin-sequence/Fennel/euclid-mullin-sequence.fennel
2026-04-30 12:34:36 -04:00

18 lines
624 B
Fennel

(do ;;; find elements of the Euclid-Mullin sequence: starting from 2,
;;; the next element is the smallest prime factor of 1 + the product
;;; of the previous elements
(io.write "2")
(var product 2)
(for [i 2 9]
(var (nextV p found) (values (+ product 1) 3 false))
; find the first prime factor of nextV
(while (and (<= (* p p) nextV) (not found))
(set found (= 0 (% nextV p)))
(when (not found) (set p (+ p 2)))
)
(when found (set nextV p))
(io.write (.. " " nextV))
(set product (* product nextV))
)
)