Added prime numbers example
This commit is contained in:
47
examples/prime/prime.q
Normal file
47
examples/prime/prime.q
Normal file
@ -0,0 +1,47 @@
|
||||
import log
|
||||
import sys
|
||||
|
||||
main() {
|
||||
n := 100
|
||||
i := 2
|
||||
|
||||
loop {
|
||||
if i > n {
|
||||
return
|
||||
}
|
||||
|
||||
if isPrime(i) == 1 {
|
||||
if i != 2 {
|
||||
sys.write(1, " ", 1)
|
||||
}
|
||||
|
||||
log.number(i)
|
||||
}
|
||||
|
||||
i += 1
|
||||
}
|
||||
}
|
||||
|
||||
isPrime(x) {
|
||||
if x == 2 {
|
||||
return 1
|
||||
}
|
||||
|
||||
if x % 2 == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
i := 3
|
||||
|
||||
loop {
|
||||
if i * i > x {
|
||||
return 1
|
||||
}
|
||||
|
||||
if x % i == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
i += 2
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user