mirror of
https://github.com/zekexiao/pocketlang.git
synced 2025-02-06 04:37:47 +08:00
26 lines
373 B
Plaintext
26 lines
373 B
Plaintext
|
|
## Prime numbers.
|
|
|
|
def is_prime(n)
|
|
if n < 2 then return false end
|
|
for i in 2..n
|
|
if n % i == 0 then return false end
|
|
end
|
|
return true
|
|
end
|
|
|
|
def get_all_primes(n)
|
|
res = []
|
|
for i in 0..n
|
|
if is_prime(i)
|
|
list_append(res, i)
|
|
end
|
|
end
|
|
return res
|
|
end
|
|
|
|
primes = get_all_primes(20)
|
|
assert(primes == [2, 3, 5, 7, 11, 13, 17, 19])
|
|
|
|
print('primes =', primes)
|