Compare commits
13 Commits
65314d52ac
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
| 72d6f4e042 | |||
| d4dc54fe59 | |||
| c7f4f3c2c4 | |||
| 6c789195e6 | |||
| 98cb2f58ea | |||
| 073276da10 | |||
| 56a094730c | |||
| 3a4a5c8849 | |||
| c0f3f730ae | |||
| 97d756bab4 | |||
| 91c9eaf1b9 | |||
| 92bb314b55 | |||
| 05e18e5aef |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
/target
|
/target
|
||||||
/indivisible.1.gz
|
/indivisible.1.gz
|
||||||
|
callgrind.out.*
|
||||||
|
|||||||
28
benchmark.sh
Executable file
28
benchmark.sh
Executable file
@@ -0,0 +1,28 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
BIN="./target/release/indivisible"
|
||||||
|
TRIALS=20
|
||||||
|
|
||||||
|
if ! [ -f "$BIN" ]
|
||||||
|
then
|
||||||
|
>&2 echo "Release build not available. Please run 'cargo build -r'."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! command -v calc &>/dev/null
|
||||||
|
then
|
||||||
|
>&2 echo "Missing 'calc' program. Please install it for this script."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Calculating primes up to 100,000,000"
|
||||||
|
TOTAL="0"
|
||||||
|
for _ in $(seq "$TRIALS")
|
||||||
|
do
|
||||||
|
TIME=$(command time -f "%e" "$BIN" 100000000 2>&1 >/dev/null)
|
||||||
|
TOTAL=$(calc "$TOTAL + $TIME")
|
||||||
|
done
|
||||||
|
|
||||||
|
AVG=$(calc "$TOTAL / $TRIALS")
|
||||||
|
|
||||||
|
echo "Average time: ${AVG}s"
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 of the base
|
|
||||||
first_use:bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CandidateGenerator {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
CandidateGenerator {
|
|
||||||
base: 0,
|
|
||||||
first_use: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn calc_base(&mut self, last_prime:u64) {
|
|
||||||
if last_prime == 2 {
|
|
||||||
self.base = 0;
|
|
||||||
self.first_use = false;
|
|
||||||
} else if last_prime == 3 {
|
|
||||||
self.base = 6;
|
|
||||||
self.first_use = true;
|
|
||||||
} else {
|
|
||||||
let modulo = last_prime % 6;
|
|
||||||
if modulo == 1 {
|
|
||||||
self.base = last_prime + 5;
|
|
||||||
self.first_use = true;
|
|
||||||
} else if modulo == 5 {
|
|
||||||
self.base = last_prime + 1;
|
|
||||||
self.first_use = false;
|
|
||||||
} else {
|
|
||||||
panic!("Invalid last prime {}" , last_prime);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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.base += 6;
|
|
||||||
}
|
|
||||||
self.first_use = !self.first_use;
|
|
||||||
|
|
||||||
val
|
|
||||||
}
|
|
||||||
}
|
|
||||||
95
src/main.rs
95
src/main.rs
@@ -16,15 +16,12 @@
|
|||||||
* 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;
|
|
||||||
use std::process;
|
use std::process;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
|
||||||
mod candidate;
|
|
||||||
use candidate::CandidateGenerator;
|
|
||||||
mod worker;
|
mod worker;
|
||||||
|
|
||||||
#[derive(StructOpt)]
|
#[derive(StructOpt)]
|
||||||
@@ -32,85 +29,53 @@ 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 = "Ordinal 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")]
|
||||||
num:u64,
|
num:usize,
|
||||||
#[structopt(short, long, name = "n", default_value = "1", help = "Number of threads to spawn")]
|
//#[structopt(short, long, name = "n", default_value = "1", help = "Number of threads to spawn")]
|
||||||
jobs:u64,
|
//jobs:u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let opts = Opt::from_args();
|
let opts = Opt::from_args();
|
||||||
|
|
||||||
let mut prime_list = VecDeque::<u64>::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_back(p.unwrap().parse().unwrap());
|
prime_list.push(p.unwrap().parse().unwrap());
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
if opts.num == 0 {
|
if opts.num < 2 {
|
||||||
eprintln!("Invalid value for num: {}", opts.num);
|
eprintln!("Invalid value for num: {}", opts.num);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.test && *prime_list.back().unwrap_or(&0) >= opts.num {
|
let prime_list = worker::work_segment(0, opts.num);
|
||||||
for i in prime_list.iter() {
|
|
||||||
if *i == opts.num {
|
if opts.test {
|
||||||
process::exit(0)
|
if *prime_list.last().unwrap() == (opts.num as u64) {
|
||||||
|
if opts.verbose {
|
||||||
|
println!("{} is prime", opts.num);
|
||||||
}
|
}
|
||||||
|
process::exit(0);
|
||||||
|
} else {
|
||||||
|
if opts.verbose {
|
||||||
|
println!("{} is composite", opts.num);
|
||||||
|
}
|
||||||
|
process::exit(1);
|
||||||
}
|
}
|
||||||
process::exit(1)
|
|
||||||
} else if !opts.test && prime_list.len() >= opts.num as usize {
|
|
||||||
let res = *prime_list.get(opts.num as usize).unwrap();
|
|
||||||
println!("{}", res);
|
|
||||||
} else {
|
} else {
|
||||||
let mut cand_gen = CandidateGenerator::new();
|
if !opts.verbose {
|
||||||
if !prime_list.is_empty() {
|
println!("{}", prime_list.last().unwrap());
|
||||||
cand_gen.calc_base(*prime_list.back().unwrap());
|
} else {
|
||||||
}
|
for p in prime_list {
|
||||||
|
println!("{}", p);
|
||||||
loop {
|
|
||||||
let cand = cand_gen.next();
|
|
||||||
if opts.test && cand > opts.num {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut is_prime = true;
|
|
||||||
for p in prime_list.iter() {
|
|
||||||
if cand % *p == 0 {
|
|
||||||
is_prime = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if is_prime {
|
|
||||||
prime_list.push_back(cand);
|
|
||||||
if opts.verbose {
|
|
||||||
println!("{}", cand);
|
|
||||||
}
|
|
||||||
|
|
||||||
if !opts.test && prime_list.len() == opts.num as usize {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if opts.test {
|
|
||||||
if *prime_list.back().unwrap() == opts.num {
|
|
||||||
process::exit(0)
|
|
||||||
} else {
|
|
||||||
process::exit(1)
|
|
||||||
}
|
|
||||||
} else if !opts.verbose {
|
|
||||||
let last_prime = *prime_list.back().unwrap();
|
|
||||||
println!("{}", last_prime);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,21 +16,68 @@
|
|||||||
* 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::cell::RefCell;
|
/**
|
||||||
use std::collections::VecDeque;
|
* @brief Work on a segment.
|
||||||
use std::rc::Rc;
|
*
|
||||||
|
* @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];
|
||||||
|
|
||||||
pub struct Worker {
|
if start < 2 && end > 2 {
|
||||||
prime_list:Rc<RefCell<VecDeque<u64>>>,
|
arr[2] = true;
|
||||||
}
|
}
|
||||||
|
if start < 3 && end > 3 {
|
||||||
|
arr[3] = true;
|
||||||
|
}
|
||||||
|
|
||||||
impl Worker {
|
let sqrt_of_num = f64::sqrt(end as f64) as usize;
|
||||||
pub fn new(primes_list:Rc<RefCell<VecDeque<u64>>>) -> Worker {
|
for x in 1..=sqrt_of_num {
|
||||||
Worker {
|
let xx4 = 4 * x * x;
|
||||||
prime_list: primes_list,
|
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];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(&mut self) {
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user