1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-18 06:34:48 +00:00
janet/examples/primes.janet
Calvin Rose 6b95326d7c First commit removing the integer number type. This should
remove some complexity and unexpected behavior around numbers in
general as all numbers are the same number type, IEEE 754 double
precision numbers. Also update examples and tests, some of which were
out of date.

Some more testing may be needed for new changes to numbers.
2018-12-27 13:05:29 -05:00

17 lines
415 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 @[])
(for i 2 n
(var isprime? true)
(def len (length list))
(for j 0 len
(def trial (get list j))
(if (zero? (% i trial)) (set isprime? false)))
(if isprime? (array/push list i)))
list)
(print (string/pretty (primes 100)))