2021-03-01 16:18:13 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2021 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
|
|
|
|
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-03-02 18:09:24 +01:00
|
|
|
use structopt::StructOpt;
|
|
|
|
|
use std::path::PathBuf;
|
2021-03-03 11:41:52 +01:00
|
|
|
use std::fs::File;
|
|
|
|
|
use std::io::{BufRead, BufReader};
|
2021-03-01 16:12:23 +01:00
|
|
|
use std::collections::VecDeque;
|
2021-02-26 18:22:20 +01:00
|
|
|
|
2021-05-05 18:17:37 +02:00
|
|
|
mod test;
|
|
|
|
|
|
2021-03-02 18:09:24 +01:00
|
|
|
#[derive(StructOpt)]
|
2021-03-03 13:24:53 +01:00
|
|
|
#[structopt(about = "A prime number generator and tester.")]
|
2021-05-05 18:17:37 +02:00
|
|
|
struct Opt
|
|
|
|
|
{
|
2021-03-03 13:24:53 +01:00
|
|
|
#[structopt(short, long, help = "Print all found primes")]
|
2021-03-02 18:09:24 +01:00
|
|
|
verbose:bool,
|
2022-03-03 16:46:31 +01:00
|
|
|
#[structopt(short, long, name = "FILE", help = "Import prime numbers from FILE")]
|
2021-03-03 11:41:52 +01:00
|
|
|
import:Option<PathBuf>,
|
2021-05-05 18:17:37 +02:00
|
|
|
#[structopt(short, long, help = "Test if n is prime instead of generation")]
|
|
|
|
|
test:bool,
|
2021-03-03 13:24:53 +01:00
|
|
|
#[structopt(help = "Ordinal of the prime to generate")]
|
2022-03-04 15:20:58 +01:00
|
|
|
n:u64,
|
2021-03-02 18:09:24 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-05 18:17:37 +02:00
|
|
|
fn main()
|
|
|
|
|
{
|
2021-03-02 18:09:24 +01:00
|
|
|
let opts = Opt::from_args();
|
2021-02-26 18:22:20 +01:00
|
|
|
|
2021-03-01 19:40:02 +01:00
|
|
|
// get the first `n` primes
|
2021-03-02 18:09:24 +01:00
|
|
|
let n = opts.n;
|
2021-05-05 18:17:37 +02:00
|
|
|
let mut primes:VecDeque<u64> = VecDeque::new();
|
2021-03-03 11:41:52 +01:00
|
|
|
|
|
|
|
|
if opts.import.is_some()
|
2021-03-02 18:09:24 +01:00
|
|
|
{
|
2021-03-03 11:41:52 +01:00
|
|
|
let in_file = File::open(opts.import.unwrap()).unwrap();
|
|
|
|
|
let reader = BufReader::new(in_file);
|
2022-03-09 16:59:30 +01:00
|
|
|
reader.lines().into_iter().for_each(|x| {
|
|
|
|
|
primes.push_back(x.unwrap().parse().unwrap());
|
|
|
|
|
});
|
2021-03-03 11:41:52 +01:00
|
|
|
}
|
2021-05-05 18:17:37 +02:00
|
|
|
|
|
|
|
|
if opts.test
|
2021-03-03 11:41:52 +01:00
|
|
|
{
|
2021-05-05 18:17:37 +02:00
|
|
|
let mut res:bool;
|
2021-05-07 17:36:38 +02:00
|
|
|
// if no primes were imported, test from beginning (2)
|
2021-05-05 18:17:37 +02:00
|
|
|
if primes.len() == 0
|
2021-03-03 11:41:52 +01:00
|
|
|
{
|
2022-03-04 15:20:58 +01:00
|
|
|
res = test::is_prime(n);
|
2021-03-03 11:41:52 +01:00
|
|
|
}
|
2021-05-07 17:36:38 +02:00
|
|
|
// `n` should be in `primes` if the last prime is larger than `n`
|
2022-03-04 15:20:58 +01:00
|
|
|
else if primes.back().unwrap() >= &(n)
|
2021-03-01 19:40:02 +01:00
|
|
|
{
|
2022-03-04 15:20:58 +01:00
|
|
|
res = primes.contains(&(n));
|
2021-05-05 18:17:37 +02:00
|
|
|
}
|
2021-05-07 17:36:38 +02:00
|
|
|
// we can memory test `n` if the last prime is >= sqrt(n)
|
2021-05-05 18:17:37 +02:00
|
|
|
else if primes.back().unwrap() >= &((n as f64).sqrt() as u64)
|
|
|
|
|
{
|
2022-03-04 15:20:58 +01:00
|
|
|
res = test::is_prime_mem(n, &primes)
|
2021-05-05 18:17:37 +02:00
|
|
|
}
|
2021-05-07 17:36:38 +02:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2021-05-05 18:17:37 +02:00
|
|
|
else
|
|
|
|
|
{
|
2022-03-04 15:20:58 +01:00
|
|
|
res = test::is_prime_mem(n, &primes);
|
2021-05-05 18:17:37 +02:00
|
|
|
if res
|
2021-03-01 19:40:02 +01:00
|
|
|
{
|
2022-03-04 15:20:58 +01:00
|
|
|
res = test::is_prime_f(n, primes.back().unwrap() + 2);
|
2021-03-01 19:40:02 +01:00
|
|
|
}
|
|
|
|
|
}
|
2021-05-07 17:36:38 +02:00
|
|
|
|
2021-05-05 18:17:37 +02:00
|
|
|
if res
|
2021-03-01 19:40:02 +01:00
|
|
|
{
|
2021-03-02 18:09:24 +01:00
|
|
|
if opts.verbose
|
|
|
|
|
{
|
2021-05-05 18:17:37 +02:00
|
|
|
println!("true");
|
2021-03-02 18:09:24 +01:00
|
|
|
}
|
2021-05-05 18:17:37 +02:00
|
|
|
std::process::exit(0);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if opts.verbose
|
|
|
|
|
{
|
|
|
|
|
println!("false");
|
|
|
|
|
}
|
|
|
|
|
std::process::exit(1);
|
2021-03-01 19:40:02 +01:00
|
|
|
}
|
|
|
|
|
}
|
2021-05-05 18:17:37 +02:00
|
|
|
else
|
2021-03-02 18:09:24 +01:00
|
|
|
{
|
2021-05-07 17:36:38 +02:00
|
|
|
// if `primes` already contains the nth prime, print it
|
2022-03-04 15:20:58 +01:00
|
|
|
if primes.len() >= n as usize
|
2021-05-05 18:17:37 +02:00
|
|
|
{
|
2022-03-04 15:20:58 +01:00
|
|
|
println!("{}", primes.get((n as usize) - 1).unwrap());
|
2021-05-05 18:17:37 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
let mut candidate:u64;
|
|
|
|
|
|
|
|
|
|
if primes.len() == 0
|
|
|
|
|
{
|
2021-05-07 17:36:38 +02:00
|
|
|
// assume 2 as a prime
|
2021-05-05 18:17:37 +02:00
|
|
|
primes.push_back(2);
|
|
|
|
|
if opts.verbose
|
|
|
|
|
{
|
|
|
|
|
println!("{}", 2);
|
|
|
|
|
}
|
|
|
|
|
candidate = 3;
|
|
|
|
|
}
|
|
|
|
|
else if primes.len() == 1
|
|
|
|
|
{
|
|
|
|
|
candidate = 3;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
candidate = *primes.back().unwrap() + 2;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-04 15:20:58 +01:00
|
|
|
while primes.len() < n as usize
|
2021-05-05 18:17:37 +02:00
|
|
|
{
|
|
|
|
|
if test::is_prime_mem(candidate, &primes)
|
|
|
|
|
{
|
|
|
|
|
primes.push_back(candidate);
|
|
|
|
|
if opts.verbose
|
|
|
|
|
{
|
|
|
|
|
println!("{}", candidate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
candidate += 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !opts.verbose
|
|
|
|
|
{
|
2022-03-04 15:20:58 +01:00
|
|
|
println!("{}", primes.get((n as usize) - 1).unwrap());
|
2021-05-05 18:17:37 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-03-02 18:09:24 +01:00
|
|
|
}
|
2021-02-26 17:58:58 +01:00
|
|
|
}
|