Move prime finding to its own module.

This commit is contained in:
2025-12-07 22:38:53 +01:00
parent c7f4f3c2c4
commit d4dc54fe59
2 changed files with 107 additions and 73 deletions

View File

@@ -16,106 +16,49 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
//use std::fs::File;
//use std::io::{BufRead, BufReader};
//use std::path::PathBuf;
use std::process;
use structopt::StructOpt;
mod worker;
#[derive(StructOpt)]
#[structopt(about = "A prime number generator and tester.")]
struct Opt {
#[structopt(short, long, help = "Print all found primes")]
verbose:bool,
#[structopt(short, long, name = "FILE", help = "Import prime numbers from FILE")]
import:Option<PathBuf>,
//#[structopt(short, long, name = "FILE", help = "Import prime numbers from FILE")]
//import:Option<PathBuf>,
#[structopt(short, long, help = "Test if num is prime instead of generation")]
test:bool,
#[structopt(help = "Max of the prime to generate or number to test for primality")]
num:u64,
#[structopt(short, long, name = "n", default_value = "1", help = "Number of threads to spawn")]
jobs:u64,
num:usize,
//#[structopt(short, long, name = "n", default_value = "1", help = "Number of threads to spawn")]
//jobs:u64,
}
const SEGMENT_SIZE:usize = 0x40000000;
fn main() {
let opts = Opt::from_args();
let mut prime_list = Vec::<usize>::new();
let mut arr = vec![false; SEGMENT_SIZE];
if opts.import.is_some() {
/*if opts.import.is_some() {
let in_file = File::open(opts.import.unwrap()).unwrap();
let reader = BufReader::new(in_file);
for p in reader.lines().into_iter() {
prime_list.push(p.unwrap().parse().unwrap());
}
}
}*/
let limit = (opts.num * 5) as usize;
if opts.num < 2 || limit > SEGMENT_SIZE {
if opts.num < 2 {
eprintln!("Invalid value for num: {}", opts.num);
process::exit(1);
}
if opts.num > 2 {
arr[2] = true;
}
if opts.num > 3 {
arr[3] = true;
}
let sqrt_of_num = f64::sqrt(opts.num as f64) as usize;
for x in 1..=sqrt_of_num {
let xx4 = 4 * x * x;
let xx3 = 3 * x * x;
for y in 1..=sqrt_of_num {
let yy = y * y;
let prime_list = worker::work_segment(0, opts.num);
let n1 = xx4 + yy;
if n1 % 12 == 1 || n1 % 12 == 5 {
arr[n1] = !arr[n1];
}
let n2 = xx3 + yy;
if n2 % 12 == 7 {
arr[n2] = !arr[n2];
}
if x > y {
let n3 = xx3 - yy;
if n3 % 12 == 11 {
arr[n3] = !arr[n3];
}
}
}
}
for i in 5..=sqrt_of_num {
if !arr[i] {
continue;
}
let mut j = i * i;
while j <= (opts.num as usize) {
arr[j] = false;
j += i * i;
}
}
for i in 2..=(opts.num as usize) {
if arr[i] {
if opts.verbose && !opts.test {
println!("{}", i);
}
prime_list.push(i);
}
}
if !opts.verbose && !opts.test {
println!("{}", prime_list.last().unwrap());
} else if opts.test {
if *prime_list.last().unwrap() == (opts.num as usize) {
if opts.test {
if *prime_list.last().unwrap() == (opts.num as u64) {
if opts.verbose {
println!("{} is prime", opts.num);
}
@@ -126,5 +69,13 @@ fn main() {
}
process::exit(1);
}
} else {
if !opts.verbose {
println!("{}", prime_list.last().unwrap());
} else {
for p in prime_list {
println!("{}", p);
}
}
}
}

83
src/worker.rs Normal file
View File

@@ -0,0 +1,83 @@
/*
* Copyright (C) 2025 Nicolás Ortega Froysa <nicolas@ortegas.org>
* Author: Nicolás Ortega Froysa <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/>.
*/
/**
* @brief Work on a segment.
*
* @param start:usize Beginning of the segment (inclusive).
* @param end:usize End of the segment (inclusive).
*
* @return List of primes found in segment.
*/
pub fn work_segment(start:usize, end:usize) -> Vec<u64> {
let mut found_primes = Vec::<u64>::new();
let mut arr = vec![false; end - start + 1];
if start < 2 && end > 2 {
arr[2] = true;
}
if start < 3 && end > 3 {
arr[3] = true;
}
let sqrt_of_num = f64::sqrt(end as f64) as usize;
for x in 1..=sqrt_of_num {
let xx4 = 4 * x * x;
let xx3 = 3 * x * x;
for y in 1..=sqrt_of_num {
let yy = y * y;
let n1 = xx4 + yy;
if n1 <= end && (n1 % 12 == 1 || n1 % 12 == 5) {
arr[n1] = !arr[n1];
}
let n2 = xx3 + yy;
if n2 <= end && n2 % 12 == 7 {
arr[n2] = !arr[n2];
}
if x > y {
let n3 = xx3 - yy;
if n3 <= end && n3 % 12 == 11 {
arr[n3] = !arr[n3];
}
}
}
}
for i in 5..=sqrt_of_num {
if !arr[i] {
continue;
}
let mut j = i * i;
while j <= end {
arr[j] = false;
j += i * i;
}
}
for i in 2..=end {
if arr[i] {
found_primes.push(i as u64);
}
}
found_primes
}