Files
monkey/examples/primes.monkey
Chuck Smith 387bb80094
Some checks failed
Build / build (push) Successful in 10m0s
Test / build (push) Failing after 15m32s
fix releaser tools
2024-03-24 12:23:19 -04:00

41 lines
534 B
Plaintext

#!./monkey-lang
prime := fn(n) {
# Corner cases
if (n <= 1) {
return false
}
if (n <= 3) {
return true
}
# This is checked so that we can skip
# middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0) {
return false
}
i := 5
while (i * i <= n) {
if (n % i == 0 || n % (i + 2) == 0) {
return false
}
i = i + 6
}
return true
}
if (len(args()) == 1) {
N := int(args()[0])
} else {
N := 100
}
n := 1
while (n < N) {
if (prime(n)) {
print(n)
}
n = n + 1
}