From 68732a6e475e80f822b4e8ff3a3ec818a257c905 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 3 May 2020 22:12:10 +0800 Subject: [PATCH] Use stepped range. --- scripts/primes.rhai | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/scripts/primes.rhai b/scripts/primes.rhai index 22defcb4..668fa250 100644 --- a/scripts/primes.rhai +++ b/scripts/primes.rhai @@ -13,18 +13,17 @@ prime_mask[1] = false; let total_primes_found = 0; for p in range(2, MAX_NUMBER_TO_CHECK) { - if prime_mask[p] { - print(p); + if !prime_mask[p] { continue; } - total_primes_found += 1; - let i = 2 * p; + print(p); - while i < MAX_NUMBER_TO_CHECK { - prime_mask[i] = false; - i += p; - } + total_primes_found += 1; + + 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.");