From caad128ad28fe9f8183d3352cb4beed722370bab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Fri, 4 Mar 2022 15:20:58 +0100 Subject: [PATCH] Switch to using u64 for `n` rather than usize --- src/main.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7e04010..bd57424 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,7 +35,7 @@ struct Opt #[structopt(short, long, help = "Test if n is prime instead of generation")] test:bool, #[structopt(help = "Ordinal of the prime to generate")] - n:usize, + n:u64, } fn main() @@ -64,17 +64,17 @@ fn main() // if no primes were imported, test from beginning (2) if primes.len() == 0 { - res = test::is_prime(n as u64); + res = test::is_prime(n); } // `n` should be in `primes` if the last prime is larger than `n` - else if primes.back().unwrap() >= &(n as u64) + else if primes.back().unwrap() >= &(n) { - res = primes.contains(&(n as u64)); + res = primes.contains(&(n)); } // 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) + res = test::is_prime_mem(n, &primes) } /* * if we have less primes than sqrt(n) then we can test all those @@ -83,10 +83,10 @@ fn main() */ else { - res = test::is_prime_mem(n as u64, &primes); + res = test::is_prime_mem(n, &primes); if res { - res = test::is_prime_f(n as u64, primes.back().unwrap() + 2); + res = test::is_prime_f(n, primes.back().unwrap() + 2); } } @@ -110,9 +110,9 @@ fn main() else { // if `primes` already contains the nth prime, print it - if primes.len() >= n + if primes.len() >= n as usize { - println!("{}", primes.get(n-1).unwrap()); + println!("{}", primes.get((n as usize) - 1).unwrap()); } else { @@ -137,7 +137,7 @@ fn main() candidate = *primes.back().unwrap() + 2; } - while primes.len() < n + while primes.len() < n as usize { if test::is_prime_mem(candidate, &primes) { @@ -153,7 +153,7 @@ fn main() if !opts.verbose { - println!("{}", primes.get(n-1).unwrap()); + println!("{}", primes.get((n as usize) - 1).unwrap()); } } }