Switch to using u64 for n
rather than usize
This commit is contained in:
parent
7c2a5e7250
commit
caad128ad2
22
src/main.rs
22
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user