Compare commits
3 Commits
92bb314b55
...
c0f3f730ae
| Author | SHA1 | Date | |
|---|---|---|---|
| c0f3f730ae | |||
| 97d756bab4 | |||
| 91c9eaf1b9 |
46
src/main.rs
46
src/main.rs
@@ -16,7 +16,6 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::collections::VecDeque;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::path::PathBuf;
|
||||
@@ -38,24 +37,24 @@ struct Opt {
|
||||
jobs:u64,
|
||||
}
|
||||
|
||||
const SEGMENT_SIZE:usize = 0x40000000;
|
||||
|
||||
fn main() {
|
||||
let opts = Opt::from_args();
|
||||
|
||||
let mut prime_list = VecDeque::<u64>::new();
|
||||
let mut arr = Vec::new();
|
||||
for _ in 0..=opts.num {
|
||||
arr.push(false);
|
||||
}
|
||||
let mut prime_list = Vec::<usize>::new();
|
||||
let mut arr = vec![false; SEGMENT_SIZE];
|
||||
|
||||
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_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);
|
||||
process::exit(1);
|
||||
}
|
||||
@@ -66,21 +65,28 @@ fn main() {
|
||||
arr[3] = true;
|
||||
}
|
||||
|
||||
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;
|
||||
if n1 <= (opts.num as usize) && (n1 % 12 == 1 || n1 % 12 == 5) {
|
||||
let sqrt_of_num = f64::sqrt(opts.num as f64) as u64;
|
||||
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) as usize;
|
||||
if n1 % 12 == 1 || n1 % 12 == 5 {
|
||||
arr[n1] = !arr[n1];
|
||||
}
|
||||
|
||||
let n2 = ((3 * x * x) + (y * y)) as usize;
|
||||
if n2 <= (opts.num as usize) && n2 % 12 == 7 {
|
||||
let n2 = (xx3 + yy) as usize;
|
||||
if n2 % 12 == 7 {
|
||||
arr[n2] = !arr[n2];
|
||||
}
|
||||
|
||||
let n3 = ((3 * x * x) - (y * y)) as usize;
|
||||
if x > y && n3 <= (opts.num as usize) && n3 % 12 == 11 {
|
||||
arr[n3] = !arr[n3];
|
||||
if x > y {
|
||||
let n3 = (xx3 - yy) as usize;
|
||||
if n3 % 12 == 11 {
|
||||
arr[n3] = !arr[n3];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,14 +108,14 @@ fn main() {
|
||||
if opts.verbose && !opts.test {
|
||||
println!("{}", i);
|
||||
}
|
||||
prime_list.push_back(i as u64);
|
||||
prime_list.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
if !opts.verbose && !opts.test {
|
||||
println!("{}", prime_list.back().unwrap());
|
||||
println!("{}", prime_list.last().unwrap());
|
||||
} else if opts.test {
|
||||
if *prime_list.back().unwrap() == opts.num {
|
||||
if *prime_list.last().unwrap() == (opts.num as usize) {
|
||||
if opts.verbose {
|
||||
println!("{} is prime", opts.num);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user