From 9d84ce14b22cdc8e6ded466444b8197d3d4c3f8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20A=2E=20Ortega=20Froysa?= Date: Fri, 7 May 2021 17:36:38 +0200 Subject: [PATCH] Added comments documenting code conditionals. --- src/main.rs | 11 +++++++++++ src/test.rs | 1 + 2 files changed, 12 insertions(+) diff --git a/src/main.rs b/src/main.rs index 1589717..92e09ca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,18 +61,26 @@ fn main() if opts.test { let mut res:bool; + // if no primes were imported, test from beginning (2) if primes.len() == 0 { res = test::is_prime(n as u64); } + // `n` should be in `primes` if the last prime is larger than `n` else if primes.back().unwrap() >= &(n as u64) { res = primes.contains(&(n as u64)); } + // we can memory test `n` if the last prime is >= sqrt(n) else if primes.back().unwrap() >= &((n as f64).sqrt() as u64) { res = test::is_prime_mem(n as u64, &primes) } + /* + * if we have less primes than sqrt(n) then we can test all those + * prior to the last prime in the list, and then begin testing odd + * numbers. + */ else { res = test::is_prime_mem(n as u64, &primes); @@ -81,6 +89,7 @@ fn main() res = test::is_prime_f(n as u64, primes.back().unwrap() + 2); } } + if res { if opts.verbose @@ -100,6 +109,7 @@ fn main() } else { + // if `primes` already contains the nth prime, print it if primes.len() >= n { println!("{}", primes.get(n-1).unwrap()); @@ -110,6 +120,7 @@ fn main() if primes.len() == 0 { + // assume 2 as a prime primes.push_back(2); if opts.verbose { diff --git a/src/test.rs b/src/test.rs index 563a60c..53a2350 100644 --- a/src/test.rs +++ b/src/test.rs @@ -35,6 +35,7 @@ pub fn is_prime_f(n:u64, b:u64) -> bool start += 1; } } + // skip even numbers else if start % 2 == 0 { start += 1;