Switch to using u64 for n rather than usize

This commit is contained in:
Nicolás A. Ortega Froysa 2022-03-04 15:20:58 +01:00
parent 7c2a5e7250
commit caad128ad2

View File

@ -35,7 +35,7 @@ struct Opt
#[structopt(short, long, help = "Test if n is prime instead of generation")] #[structopt(short, long, help = "Test if n is prime instead of generation")]
test:bool, test:bool,
#[structopt(help = "Ordinal of the prime to generate")] #[structopt(help = "Ordinal of the prime to generate")]
n:usize, n:u64,
} }
fn main() fn main()
@ -64,17 +64,17 @@ fn main()
// if no primes were imported, test from beginning (2) // if no primes were imported, test from beginning (2)
if primes.len() == 0 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` // `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) // we can memory test `n` if the last prime is >= sqrt(n)
else if primes.back().unwrap() >= &((n as f64).sqrt() as u64) 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 * if we have less primes than sqrt(n) then we can test all those
@ -83,10 +83,10 @@ fn main()
*/ */
else else
{ {
res = test::is_prime_mem(n as u64, &primes); res = test::is_prime_mem(n, &primes);
if res 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 else
{ {
// if `primes` already contains the nth prime, print it // 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 else
{ {
@ -137,7 +137,7 @@ fn main()
candidate = *primes.back().unwrap() + 2; candidate = *primes.back().unwrap() + 2;
} }
while primes.len() < n while primes.len() < n as usize
{ {
if test::is_prime_mem(candidate, &primes) if test::is_prime_mem(candidate, &primes)
{ {
@ -153,7 +153,7 @@ fn main()
if !opts.verbose if !opts.verbose
{ {
println!("{}", primes.get(n-1).unwrap()); println!("{}", primes.get((n as usize) - 1).unwrap());
} }
} }
} }