Compare commits

...

3 Commits

Author SHA1 Message Date
c0f3f730ae Optimize algorithm. 2025-12-05 15:41:26 +01:00
97d756bab4 Fix subtraction overflow. 2025-12-05 14:29:06 +01:00
91c9eaf1b9 Set fixed segment (array) size. 2025-12-05 14:27:22 +01:00

View File

@@ -16,7 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
use std::collections::VecDeque;
use std::fs::File; use std::fs::File;
use std::io::{BufRead, BufReader}; use std::io::{BufRead, BufReader};
use std::path::PathBuf; use std::path::PathBuf;
@@ -38,24 +37,24 @@ struct Opt {
jobs:u64, jobs:u64,
} }
const SEGMENT_SIZE:usize = 0x40000000;
fn main() { fn main() {
let opts = Opt::from_args(); let opts = Opt::from_args();
let mut prime_list = VecDeque::<u64>::new(); let mut prime_list = Vec::<usize>::new();
let mut arr = Vec::new(); let mut arr = vec![false; SEGMENT_SIZE];
for _ in 0..=opts.num {
arr.push(false);
}
if opts.import.is_some() { if opts.import.is_some() {
let in_file = File::open(opts.import.unwrap()).unwrap(); let in_file = File::open(opts.import.unwrap()).unwrap();
let reader = BufReader::new(in_file); let reader = BufReader::new(in_file);
for p in reader.lines().into_iter() { for p in reader.lines().into_iter() {
prime_list.push_back(p.unwrap().parse().unwrap()); prime_list.push(p.unwrap().parse().unwrap());
} }
} }
if opts.num < 2 { let limit = (opts.num * 5) as usize;
if opts.num < 2 || limit > SEGMENT_SIZE {
eprintln!("Invalid value for num: {}", opts.num); eprintln!("Invalid value for num: {}", opts.num);
process::exit(1); process::exit(1);
} }
@@ -66,21 +65,28 @@ fn main() {
arr[3] = true; arr[3] = true;
} }
for x in 1..=(f64::sqrt(opts.num as f64) as u64 + 1) { let sqrt_of_num = f64::sqrt(opts.num as f64) as u64;
for y in 1..=(f64::sqrt(opts.num as f64) as u64 + 1) { for x in 1..=sqrt_of_num {
let n1 = ((4 * x * x) + (y * y)) as usize; let xx4 = 4 * x * x;
if n1 <= (opts.num as usize) && (n1 % 12 == 1 || n1 % 12 == 5) { let xx3 = 3 * x * x;
for y in 1..=sqrt_of_num {
let yy = y * y;
let n1 = (xx4 + yy) as usize;
if n1 % 12 == 1 || n1 % 12 == 5 {
arr[n1] = !arr[n1]; arr[n1] = !arr[n1];
} }
let n2 = ((3 * x * x) + (y * y)) as usize; let n2 = (xx3 + yy) as usize;
if n2 <= (opts.num as usize) && n2 % 12 == 7 { if n2 % 12 == 7 {
arr[n2] = !arr[n2]; arr[n2] = !arr[n2];
} }
let n3 = ((3 * x * x) - (y * y)) as usize; if x > y {
if x > y && n3 <= (opts.num as usize) && n3 % 12 == 11 { let n3 = (xx3 - yy) as usize;
arr[n3] = !arr[n3]; if n3 % 12 == 11 {
arr[n3] = !arr[n3];
}
} }
} }
} }
@@ -102,14 +108,14 @@ fn main() {
if opts.verbose && !opts.test { if opts.verbose && !opts.test {
println!("{}", i); println!("{}", i);
} }
prime_list.push_back(i as u64); prime_list.push(i);
} }
} }
if !opts.verbose && !opts.test { if !opts.verbose && !opts.test {
println!("{}", prime_list.back().unwrap()); println!("{}", prime_list.last().unwrap());
} else if opts.test { } else if opts.test {
if *prime_list.back().unwrap() == opts.num { if *prime_list.last().unwrap() == (opts.num as usize) {
if opts.verbose { if opts.verbose {
println!("{} is prime", opts.num); println!("{} is prime", opts.num);
} }