1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-24 17:27:18 +00:00
janet/examples/primes.dst
Calvin Rose 510feeed7f Allow marshaling of more functions for core.
Fix indentation in some files.
2018-08-22 21:41:25 -04:00

15 lines
400 B
Plaintext

# Return an array of primes. This is a trivial and extremely naive algorithm.
(defn primes
"Returns a list of prime numbers less than n."
[n]
(def list @[])
(loop [i :range [2 n]]
(var isprime? true)
(def len (length list))
(loop [j :range [0 len]]
(def trial (get list j))
(if (zero? (% i trial)) (:= isprime? false)))
(if isprime? (array.push list i)))
list)