Files
indivisible-rs/src/main.rs

123 lines
3.2 KiB
Rust
Raw Normal View History

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/>.
*/
2021-03-03 11:41:52 +01:00
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use std::process;
use structopt::StructOpt;
2021-02-26 18:22:20 +01:00
#[derive(StructOpt)]
2021-03-03 13:24:53 +01:00
#[structopt(about = "A prime number generator and tester.")]
struct Opt {
2021-03-03 13:24:53 +01:00
#[structopt(short, long, help = "Print all found primes")]
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>,
#[structopt(short, long, help = "Test if num is prime instead of generation")]
test:bool,
#[structopt(help = "Ordinal of the prime to generate or number to test for primality")]
2025-12-05 13:55:06 +01:00
num:u64,
#[structopt(short, long, name = "n", default_value = "1", help = "Number of threads to spawn")]
jobs:u64,
}
2025-12-05 14:27:22 +01:00
const SEGMENT_SIZE:usize = 0x40000000;
fn main() {
let opts = Opt::from_args();
2021-02-26 18:22:20 +01:00
2025-12-05 14:27:22 +01:00
let mut prime_list = Vec::<usize>::new();
let mut arr = vec![false; SEGMENT_SIZE];
2021-03-03 11:41:52 +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);
for p in reader.lines().into_iter() {
2025-12-05 14:27:22 +01:00
prime_list.push(p.unwrap().parse().unwrap());
}
}
2025-12-05 14:27:22 +01:00
if opts.num < 2 || (opts.num as usize) > SEGMENT_SIZE {
eprintln!("Invalid value for num: {}", opts.num);
process::exit(1);
}
2025-12-05 13:16:37 +01:00
if opts.num > 2 {
arr[2] = true;
}
if opts.num > 3 {
arr[3] = true;
}
2025-12-05 13:16:37 +01:00
for x in 1..=(f64::sqrt(opts.num as f64) as u64 + 1) {
for y in 1..=(f64::sqrt(opts.num as f64) as u64 + 1) {
let n1 = ((4 * x * x) + (y * y)) as usize;
2025-12-05 13:55:06 +01:00
if n1 <= (opts.num as usize) && (n1 % 12 == 1 || n1 % 12 == 5) {
2025-12-05 13:16:37 +01:00
arr[n1] = !arr[n1];
}
2025-12-04 09:38:08 +01:00
2025-12-05 13:16:37 +01:00
let n2 = ((3 * x * x) + (y * y)) as usize;
2025-12-05 13:55:06 +01:00
if n2 <= (opts.num as usize) && n2 % 12 == 7 {
2025-12-05 13:16:37 +01:00
arr[n2] = !arr[n2];
2025-12-04 11:05:40 +01:00
}
2025-12-05 13:16:37 +01:00
let n3 = ((3 * x * x) - (y * y)) as usize;
2025-12-05 13:55:06 +01:00
if x > y && n3 <= (opts.num as usize) && n3 % 12 == 11 {
2025-12-05 13:16:37 +01:00
arr[n3] = !arr[n3];
}
2025-12-05 13:16:37 +01:00
}
}
2025-12-05 13:16:37 +01:00
for i in 5..=(f64::sqrt(opts.num as f64) as u64 + 1) as usize {
if !arr[i as usize] {
continue;
}
2025-12-04 11:05:40 +01:00
2025-12-05 13:16:37 +01:00
let mut j = i * i;
2025-12-05 13:55:06 +01:00
while j <= (opts.num as usize) {
2025-12-05 13:16:37 +01:00
arr[j] = false;
j += i * i;
}
2025-12-05 13:16:37 +01:00
}
2025-12-05 13:55:06 +01:00
for i in 2..=(opts.num as usize) {
2025-12-05 13:16:37 +01:00
if arr[i] {
2025-12-05 13:55:06 +01:00
if opts.verbose && !opts.test {
println!("{}", i);
}
2025-12-05 14:27:22 +01:00
prime_list.push(i);
2025-12-04 09:38:08 +01:00
}
}
2025-12-05 13:55:06 +01:00
if !opts.verbose && !opts.test {
2025-12-05 14:27:22 +01:00
println!("{}", prime_list.last().unwrap());
2025-12-05 13:55:06 +01:00
} else if opts.test {
2025-12-05 14:27:22 +01:00
if *prime_list.last().unwrap() == (opts.num as usize) {
2025-12-05 13:55:06 +01:00
if opts.verbose {
println!("{} is prime", opts.num);
}
process::exit(0);
} else {
if opts.verbose {
println!("{} is composite", opts.num);
}
process::exit(1);
}
}
2021-02-26 17:58:58 +01:00
}