Switch to (segmented) sieve of Eratosthenes.
This commit is contained in:
50
src/main.rs
50
src/main.rs
@@ -16,9 +16,9 @@
|
|||||||
* 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::fs::File;
|
use std::fs::File;
|
||||||
//use std::io::{BufRead, BufReader};
|
use std::io::{BufRead, BufReader};
|
||||||
//use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process;
|
use std::process;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
|
||||||
@@ -29,8 +29,8 @@ mod worker;
|
|||||||
struct Opt {
|
struct Opt {
|
||||||
#[structopt(short, long, help = "Print all found primes")]
|
#[structopt(short, long, help = "Print all found primes")]
|
||||||
verbose:bool,
|
verbose:bool,
|
||||||
//#[structopt(short, long, name = "FILE", help = "Import prime numbers from FILE")]
|
#[structopt(short, long, name = "FILE", help = "Import prime numbers from FILE")]
|
||||||
//import:Option<PathBuf>,
|
import:Option<PathBuf>,
|
||||||
#[structopt(short, long, help = "Test if num is prime instead of generation")]
|
#[structopt(short, long, help = "Test if num is prime instead of generation")]
|
||||||
test:bool,
|
test:bool,
|
||||||
#[structopt(help = "Max of the prime to generate or number to test for primality")]
|
#[structopt(help = "Max of the prime to generate or number to test for primality")]
|
||||||
@@ -39,23 +39,47 @@ struct Opt {
|
|||||||
//jobs:u64,
|
//jobs:u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const SEGMENT_SIZE:usize = 0x100000000;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let opts = Opt::from_args();
|
let opts = Opt::from_args();
|
||||||
|
let mut prime_list = Vec::new();
|
||||||
|
|
||||||
/*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(p.unwrap().parse().unwrap());
|
prime_list.push(p.unwrap().parse().unwrap());
|
||||||
}
|
}
|
||||||
}*/
|
}
|
||||||
|
|
||||||
if opts.num < 2 {
|
if opts.num < 2 {
|
||||||
eprintln!("Invalid value for num: {}", opts.num);
|
eprintln!("Invalid value for num: {}", opts.num);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let prime_list = worker::work_segment(0, opts.num);
|
let mut start:usize = if prime_list.is_empty() {
|
||||||
|
2
|
||||||
|
} else {
|
||||||
|
*prime_list.last().unwrap() as usize
|
||||||
|
};
|
||||||
|
while start < opts.num {
|
||||||
|
let end = if start + SEGMENT_SIZE < opts.num {
|
||||||
|
start + SEGMENT_SIZE
|
||||||
|
} else {
|
||||||
|
opts.num + 1
|
||||||
|
};
|
||||||
|
let mut new_primes = worker::work_segment(&prime_list, start, end);
|
||||||
|
|
||||||
|
if opts.verbose {
|
||||||
|
for p in &new_primes {
|
||||||
|
println!("{}", *p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
prime_list.append(&mut new_primes);
|
||||||
|
|
||||||
|
start += SEGMENT_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
if opts.test {
|
if opts.test {
|
||||||
if *prime_list.last().unwrap() == (opts.num as u64) {
|
if *prime_list.last().unwrap() == (opts.num as u64) {
|
||||||
@@ -69,13 +93,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
} else {
|
} else if !opts.verbose {
|
||||||
if !opts.verbose {
|
println!("{}", prime_list.last().unwrap());
|
||||||
println!("{}", prime_list.last().unwrap());
|
|
||||||
} else {
|
|
||||||
for p in prime_list {
|
|
||||||
println!("{}", p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,62 +20,35 @@
|
|||||||
* @brief Work on a segment.
|
* @brief Work on a segment.
|
||||||
*
|
*
|
||||||
* @param start:usize Beginning of the segment (inclusive).
|
* @param start:usize Beginning of the segment (inclusive).
|
||||||
* @param end:usize End of the segment (inclusive).
|
* @param end:usize End of the segment (exclusive).
|
||||||
*
|
*
|
||||||
* @return List of primes found in segment.
|
* @return List of primes found in segment.
|
||||||
*/
|
*/
|
||||||
pub fn work_segment(start:usize, end:usize) -> Vec<u64> {
|
pub fn work_segment(known_primes:&Vec<u64>, start:usize, end:usize) -> Vec<u64> {
|
||||||
let mut found_primes = Vec::<u64>::new();
|
let mut sieve = vec![true; end - start];
|
||||||
let mut arr = vec![false; end - start + 1];
|
let mut found_primes = Vec::new();
|
||||||
|
|
||||||
if start < 2 && end > 2 {
|
for p in known_primes {
|
||||||
arr[2] = true;
|
let prime = *p as usize;
|
||||||
}
|
let mut mult = prime * prime;
|
||||||
if start < 3 && end > 3 {
|
while mult < end {
|
||||||
arr[3] = true;
|
if mult > start {
|
||||||
}
|
sieve[mult - start] = false;
|
||||||
|
|
||||||
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];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
mult += prime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for i in 5..=sqrt_of_num {
|
for i in 0..(end - start) {
|
||||||
if !arr[i] {
|
if sieve[i] {
|
||||||
continue;
|
let prime = i + start;
|
||||||
}
|
found_primes.push(prime as u64);
|
||||||
|
|
||||||
let mut j = i * i;
|
let mut mult = prime * prime;
|
||||||
while j <= end {
|
while mult < end {
|
||||||
arr[j] = false;
|
sieve[mult - start] = false;
|
||||||
j += i * i;
|
mult += prime;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for i in 2..=end {
|
|
||||||
if arr[i] {
|
|
||||||
found_primes.push(i as u64);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user