Use stepped range.

This commit is contained in:
Stephen Chung 2020-05-03 22:12:10 +08:00
parent d83b829810
commit 68732a6e47

View File

@ -13,18 +13,17 @@ prime_mask[1] = false;
let total_primes_found = 0; let total_primes_found = 0;
for p in range(2, MAX_NUMBER_TO_CHECK) { for p in range(2, MAX_NUMBER_TO_CHECK) {
if prime_mask[p] { if !prime_mask[p] { continue; }
print(p);
total_primes_found += 1; print(p);
let i = 2 * p;
while i < MAX_NUMBER_TO_CHECK { total_primes_found += 1;
prime_mask[i] = false;
i += p; for i in range(2 * p, MAX_NUMBER_TO_CHECK, p) {
} prime_mask[i] = false;
i += p;
} }
} }
print("Total " + total_primes_found + " primes."); print("Total " + total_primes_found + " primes <= " + MAX_NUMBER_TO_CHECK);
print("Run time = " + now.elapsed() + " seconds."); print("Run time = " + now.elapsed() + " seconds.");