Impelment CandidateGenerator.
This commit is contained in:
62
src/candidate.rs
Normal file
62
src/candidate.rs
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
pub struct CandidateGenerator {
|
||||||
|
base:u64,
|
||||||
|
first_use:bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CandidateGenerator {
|
||||||
|
pub fn new() -> CandidateGenerator {
|
||||||
|
CandidateGenerator {
|
||||||
|
base: 0,
|
||||||
|
first_use: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn next(&mut self) -> u64 {
|
||||||
|
/*
|
||||||
|
* All primes, except 2 and 3, will be equal to (n * 6 ± 1). This avoids
|
||||||
|
* multiples of three, optimizing our counting.
|
||||||
|
*/
|
||||||
|
let val;
|
||||||
|
|
||||||
|
if self.base != 0 {
|
||||||
|
if self.first_use {
|
||||||
|
val = self.base - 1;
|
||||||
|
} else {
|
||||||
|
val = self.base + 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if self.first_use {
|
||||||
|
val = 2;
|
||||||
|
} else {
|
||||||
|
val = 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.first_use {
|
||||||
|
self.first_use = false;
|
||||||
|
} else {
|
||||||
|
self.first_use = true;
|
||||||
|
self.base += 6;
|
||||||
|
}
|
||||||
|
|
||||||
|
val
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -25,6 +25,8 @@ use std::process;
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
|
||||||
|
mod candidate;
|
||||||
|
use candidate::CandidateGenerator;
|
||||||
mod worker;
|
mod worker;
|
||||||
|
|
||||||
#[derive(StructOpt)]
|
#[derive(StructOpt)]
|
||||||
@@ -76,5 +78,10 @@ fn main() {
|
|||||||
let res = *primes_list.borrow().get(opts.num as usize).unwrap();
|
let res = *primes_list.borrow().get(opts.num as usize).unwrap();
|
||||||
println!("{}", res);
|
println!("{}", res);
|
||||||
} else {
|
} else {
|
||||||
|
let mut cand_gen = CandidateGenerator::new();
|
||||||
|
|
||||||
|
while primes_list.borrow().len() < opts.num as usize {
|
||||||
|
let cand = cand_gen.next();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user