2021-03-01 16:18:13 +01:00
|
|
|
/*
|
2025-12-04 08:59:49 +01:00
|
|
|
* Copyright (C) 2025 Nicolás Ortega Froysa <nicolas@ortegas.org>
|
|
|
|
|
* Author: Nicolás Ortega Froysa <nicolas@ortegas.org>
|
2021-03-01 16:18:13 +01:00
|
|
|
*
|
|
|
|
|
* 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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-12-03 21:39:22 +01:00
|
|
|
use std::cell::RefCell;
|
|
|
|
|
use std::collections::VecDeque;
|
2021-03-03 11:41:52 +01:00
|
|
|
use std::fs::File;
|
|
|
|
|
use std::io::{BufRead, BufReader};
|
2025-12-03 21:39:22 +01:00
|
|
|
use std::path::PathBuf;
|
2025-12-04 08:58:57 +01:00
|
|
|
use std::process;
|
2025-12-03 21:39:22 +01:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
use structopt::StructOpt;
|
2021-02-26 18:22:20 +01:00
|
|
|
|
2025-12-04 09:38:08 +01:00
|
|
|
mod candidate;
|
|
|
|
|
use candidate::CandidateGenerator;
|
2025-12-03 21:39:22 +01:00
|
|
|
mod worker;
|
2021-05-05 18:17:37 +02:00
|
|
|
|
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.")]
|
2025-12-03 21:39:22 +01: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>,
|
2025-12-04 08:58:57 +01:00
|
|
|
#[structopt(short, long, help = "Test if num is prime instead of generation")]
|
2021-05-05 18:17:37 +02:00
|
|
|
test:bool,
|
2025-12-03 21:39:22 +01:00
|
|
|
#[structopt(help = "Ordinal of the prime to generate or number to test for primality")]
|
2025-12-04 08:58:57 +01:00
|
|
|
num:u64,
|
2025-12-03 21:39:22 +01:00
|
|
|
#[structopt(short, long, name = "n", help = "Number of threads to spawn")]
|
|
|
|
|
jobs:Option<u64>,
|
2021-03-02 18:09:24 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 21:39:22 +01:00
|
|
|
fn main() {
|
2021-03-02 18:09:24 +01:00
|
|
|
let opts = Opt::from_args();
|
2021-02-26 18:22:20 +01:00
|
|
|
|
2025-12-04 11:26:10 +01:00
|
|
|
let prime_list = Rc::new(RefCell::new(VecDeque::<u64>::new()));
|
2021-03-03 11:41:52 +01:00
|
|
|
|
2025-12-03 21:39:22 +01:00
|
|
|
if opts.import.is_some() {
|
2021-03-03 11:41:52 +01:00
|
|
|
let in_file = File::open(opts.import.unwrap()).unwrap();
|
|
|
|
|
let reader = BufReader::new(in_file);
|
2025-12-03 21:39:22 +01:00
|
|
|
for p in reader.lines().into_iter() {
|
2025-12-04 11:26:10 +01:00
|
|
|
prime_list.borrow_mut().push_back(p.unwrap().parse().unwrap());
|
2021-03-01 19:40:02 +01:00
|
|
|
}
|
|
|
|
|
}
|
2021-05-05 18:17:37 +02:00
|
|
|
|
2025-12-03 21:39:22 +01:00
|
|
|
let jobs = match opts.jobs {
|
|
|
|
|
Some(n) => n,
|
|
|
|
|
None => 1, // TODO: use number of CPUs
|
|
|
|
|
};
|
2025-12-04 08:58:57 +01:00
|
|
|
|
|
|
|
|
if opts.num == 0 {
|
|
|
|
|
eprintln!("Invalid value for num: {}", opts.num);
|
|
|
|
|
process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-04 11:26:10 +01:00
|
|
|
if opts.test && *prime_list.borrow().back().unwrap_or(&0) >= opts.num {
|
|
|
|
|
for i in prime_list.borrow().iter() {
|
2025-12-04 08:58:57 +01:00
|
|
|
if *i == opts.num {
|
|
|
|
|
process::exit(0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
process::exit(1)
|
2025-12-04 11:26:10 +01:00
|
|
|
} else if !opts.test && prime_list.borrow().len() >= opts.num as usize {
|
|
|
|
|
let res = *prime_list.borrow().get(opts.num as usize).unwrap();
|
2025-12-04 08:58:57 +01:00
|
|
|
println!("{}", res);
|
|
|
|
|
} else {
|
2025-12-04 09:38:08 +01:00
|
|
|
let mut cand_gen = CandidateGenerator::new();
|
2025-12-04 11:26:10 +01:00
|
|
|
if !prime_list.borrow().is_empty() {
|
|
|
|
|
cand_gen.calc_base(*prime_list.borrow().back().unwrap());
|
2025-12-04 11:24:40 +01:00
|
|
|
}
|
2025-12-04 09:38:08 +01:00
|
|
|
|
2025-12-04 11:05:40 +01:00
|
|
|
loop {
|
2025-12-04 09:38:08 +01:00
|
|
|
let cand = cand_gen.next();
|
2025-12-04 11:05:40 +01:00
|
|
|
if opts.test && cand > opts.num {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-12-04 09:46:06 +01:00
|
|
|
|
2025-12-04 11:05:40 +01:00
|
|
|
let mut is_prime = true;
|
2025-12-04 11:26:10 +01:00
|
|
|
for p in prime_list.borrow().iter() {
|
2025-12-04 11:05:40 +01:00
|
|
|
if cand % *p == 0 {
|
2025-12-04 09:46:06 +01:00
|
|
|
is_prime = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if is_prime {
|
2025-12-04 11:26:10 +01:00
|
|
|
prime_list.borrow_mut().push_back(cand);
|
2025-12-04 09:46:06 +01:00
|
|
|
if opts.verbose {
|
|
|
|
|
println!("{}", cand);
|
|
|
|
|
}
|
2025-12-04 11:05:40 +01:00
|
|
|
|
2025-12-04 11:26:10 +01:00
|
|
|
if !opts.test && prime_list.borrow().len() == opts.num as usize {
|
2025-12-04 11:05:40 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2025-12-04 09:46:06 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-04 11:05:40 +01:00
|
|
|
if opts.test {
|
2025-12-04 11:26:10 +01:00
|
|
|
if *prime_list.borrow().back().unwrap() == opts.num {
|
2025-12-04 11:05:40 +01:00
|
|
|
process::exit(0)
|
|
|
|
|
} else {
|
|
|
|
|
process::exit(1)
|
|
|
|
|
}
|
|
|
|
|
} else if !opts.verbose {
|
2025-12-04 11:26:10 +01:00
|
|
|
let last_prime = *prime_list.borrow().back().unwrap();
|
2025-12-04 09:46:06 +01:00
|
|
|
println!("{}", last_prime);
|
2025-12-04 09:38:08 +01:00
|
|
|
}
|
2025-12-04 08:58:57 +01:00
|
|
|
}
|
2021-02-26 17:58:58 +01:00
|
|
|
}
|