22 lines
674 B
Mojo
22 lines
674 B
Mojo
# probe: the sieve of Eratosthenes with a List[Bool]
|
|
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
|
|
# expect-stdout: 15 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
|
|
def primes_below(n: Int) -> List[Int]:
|
|
var is_prime = List[Bool]()
|
|
for _ in range(n):
|
|
is_prime.append(True)
|
|
var out = List[Int]()
|
|
for i in range(2, n):
|
|
if is_prime[i]:
|
|
out.append(i)
|
|
var j = i * i
|
|
while j < n:
|
|
is_prime[j] = False
|
|
j += i
|
|
return out^
|
|
|
|
|
|
def main():
|
|
var ps = primes_below(50)
|
|
print(len(ps), ps)
|