Compare commits
34 Commits
master
...
98cb2f58ea
| Author | SHA1 | Date | |
|---|---|---|---|
| 98cb2f58ea | |||
| 073276da10 | |||
| 56a094730c | |||
| 3a4a5c8849 | |||
| c0f3f730ae | |||
| 97d756bab4 | |||
| 91c9eaf1b9 | |||
| 92bb314b55 | |||
| 05e18e5aef | |||
| 65314d52ac | |||
| 00da4333bf | |||
| bde097302b | |||
| ed209116c4 | |||
| 63260b754e | |||
| e4844db5d6 | |||
| dfdadb9860 | |||
| 744efa105b | |||
| 22fe0953ca | |||
| e17ebf47c3 | |||
| 9a7e303ed8 | |||
| 6894d250dd | |||
| 23c8dd694f | |||
| 04d76e53bd | |||
| 449a4bd2a6 | |||
| 8a81df1006 | |||
| 8b92ab252b | |||
| 1bc1a02761 | |||
| 82d75227c4 | |||
| 0797c7419d | |||
| 2965336290 | |||
| 1b6ad7c797 | |||
| f88c221a67 | |||
| fed8fe9ba8 | |||
| ceb7e5974e |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
/target
|
||||
/indivisible.1.gz
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
image: "rust:latest"
|
||||
|
||||
stages:
|
||||
- build
|
||||
- test
|
||||
|
||||
build-job:
|
||||
stage: build
|
||||
artifacts:
|
||||
paths:
|
||||
- target/release/indivisible
|
||||
script:
|
||||
- cargo build --release
|
||||
|
||||
test-job:
|
||||
stage: test
|
||||
script:
|
||||
- ./test.sh
|
||||
|
||||
include:
|
||||
- template: Security/License-Scanning.gitlab-ci.yml
|
||||
193
Cargo.lock
generated
193
Cargo.lock
generated
@@ -1,12 +1,12 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "ansi_term"
|
||||
version = "0.11.0"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
|
||||
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
@@ -22,29 +22,17 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.2.1"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "2.33.3"
|
||||
version = "2.34.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
|
||||
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
|
||||
dependencies = [
|
||||
"ansi_term",
|
||||
"atty",
|
||||
@@ -55,70 +43,20 @@ dependencies = [
|
||||
"vec_map",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-channel"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e54ea8bc3fb1ee042f5aace6e3c6e025d3874866da222930f70ce62aceba0bfa"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-deque"
|
||||
version = "0.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6455c0ca19f0d2fbf751b908d5c55c1f5cbc65e03c4225427254b46890bdde1e"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"crossbeam-epoch",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-epoch"
|
||||
version = "0.9.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c00d6d2ea26e8b151d99093005cb442fb9a37aeaca582a03ec70946f49ab5ed9"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"crossbeam-utils",
|
||||
"lazy_static",
|
||||
"memoffset",
|
||||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.8.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b5e5bed1f1c269533fa816a0a5492b3545209a205ca1a54842be180eb63a16a6"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.3.2"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "87cbf45460356b7deeb5e3415b5563308c0a9b057c85e12b06ad551f98d0a6ac"
|
||||
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
|
||||
dependencies = [
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.1.18"
|
||||
version = "0.1.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c"
|
||||
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
@@ -127,40 +65,20 @@ dependencies = [
|
||||
name = "indivisible"
|
||||
version = "2.0.0"
|
||||
dependencies = [
|
||||
"rayon",
|
||||
"structopt",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.87"
|
||||
version = "0.2.178"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "265d751d31d6780a3f956bb5b8022feba2d94eeee5a84ba64f4212eedca42213"
|
||||
|
||||
[[package]]
|
||||
name = "memoffset"
|
||||
version = "0.6.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num_cpus"
|
||||
version = "1.13.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1"
|
||||
dependencies = [
|
||||
"hermit-abi",
|
||||
"libc",
|
||||
]
|
||||
checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error"
|
||||
@@ -188,53 +106,22 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.24"
|
||||
version = "1.0.103"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
|
||||
checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8"
|
||||
dependencies = [
|
||||
"unicode-xid",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.9"
|
||||
version = "1.0.42"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
|
||||
checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rayon"
|
||||
version = "1.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c06aca804d41dbc8ba42dfd964f0d01334eceb64314b9ecf7c5fad5188a06d90"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"crossbeam-deque",
|
||||
"either",
|
||||
"rayon-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rayon-core"
|
||||
version = "1.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d78120e2c850279833f1dd3582f730c4ab53ed95aeaaaa862a2a5c71b1656d8e"
|
||||
dependencies = [
|
||||
"crossbeam-channel",
|
||||
"crossbeam-deque",
|
||||
"crossbeam-utils",
|
||||
"lazy_static",
|
||||
"num_cpus",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.8.0"
|
||||
@@ -243,9 +130,9 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
|
||||
|
||||
[[package]]
|
||||
name = "structopt"
|
||||
version = "0.3.21"
|
||||
version = "0.3.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5277acd7ee46e63e5168a80734c9f6ee81b1367a7d8772a2d765df2a3705d28c"
|
||||
checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"lazy_static",
|
||||
@@ -254,9 +141,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "structopt-derive"
|
||||
version = "0.4.14"
|
||||
version = "0.4.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5ba9cdfda491b814720b6b06e0cac513d922fc407582032e8706e9f137976f90"
|
||||
checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro-error",
|
||||
@@ -267,13 +154,13 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.60"
|
||||
version = "1.0.109"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081"
|
||||
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -286,22 +173,22 @@ dependencies = [
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-segmentation"
|
||||
version = "1.7.1"
|
||||
name = "unicode-ident"
|
||||
version = "1.0.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796"
|
||||
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-segmentation"
|
||||
version = "1.12.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.1.8"
|
||||
version = "0.1.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
|
||||
checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af"
|
||||
|
||||
[[package]]
|
||||
name = "vec_map"
|
||||
@@ -311,9 +198,9 @@ checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.2"
|
||||
version = "0.9.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
|
||||
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
|
||||
@@ -4,12 +4,9 @@ version = "2.0.0"
|
||||
authors = ["Nicolás Ortega Froysa <nicolas@ortegas.org>"]
|
||||
description = "A prime number generator and tester"
|
||||
edition = "2018"
|
||||
readme = "README"
|
||||
readme = "README.md"
|
||||
license = "GPL-3.0"
|
||||
keywords = ["primes", "math"]
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
keywords = [ "primes", "math" ]
|
||||
|
||||
[dependencies]
|
||||
structopt = "0.3.13"
|
||||
rayon = "1.5"
|
||||
structopt = "0.3"
|
||||
|
||||
@@ -38,8 +38,7 @@ indivisible -i ./primes 600
|
||||
## Legacy
|
||||
|
||||
This project was originally written in C. It can be found in the
|
||||
[indivisible-legacy repository](https://gitlab.com/naortega/Indivisible-legacy)
|
||||
on my GitLab.
|
||||
[indivisible-legacy repository](https://code.ortegas.org/nortega/indivisible-legacy).
|
||||
|
||||
## License
|
||||
|
||||
|
||||
6
TODO
6
TODO
@@ -1,6 +0,0 @@
|
||||
============
|
||||
*** TODO ***
|
||||
============
|
||||
Technical:
|
||||
- Use bigints (maybe GMP or another similar library)
|
||||
- Parallelize computations
|
||||
22
benchmark.sh
Executable file
22
benchmark.sh
Executable file
@@ -0,0 +1,22 @@
|
||||
#!/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
|
||||
|
||||
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"
|
||||
9
install
9
install
@@ -6,11 +6,12 @@ then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
PREFIX="/usr/local"
|
||||
PREFIX="${PREFIX:-/usr/local}"
|
||||
|
||||
echo "Installing ./target/release/indivisible to $PREFIX/bin/"
|
||||
install -m 755 ./target/release/indivisible $PREFIX/bin/
|
||||
gzip -c indivisible.1 > indivisible.1.gz
|
||||
install -Dm755 ./target/release/indivisible "$PREFIX/bin/indivisible"
|
||||
gzip -k indivisible.1
|
||||
echo "Installing manpage to $PREFIX/share/man/man1/"
|
||||
install -m 644 indivisible.1.gz $PREFIX/share/man/man1/
|
||||
install -Dm644 ./indivisible.1.gz "$PREFIX/share/man/man1/indivisible.1.gz"
|
||||
# clean up compressed file
|
||||
rm indivisible.1.gz
|
||||
|
||||
197
src/main.rs
197
src/main.rs
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||
* 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
|
||||
@@ -16,142 +16,115 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use structopt::StructOpt;
|
||||
use std::path::PathBuf;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::collections::VecDeque;
|
||||
|
||||
mod test;
|
||||
use std::path::PathBuf;
|
||||
use std::process;
|
||||
use structopt::StructOpt;
|
||||
|
||||
#[derive(StructOpt)]
|
||||
#[structopt(about = "A prime number generator and tester.")]
|
||||
struct Opt
|
||||
{
|
||||
struct Opt {
|
||||
#[structopt(short, long, help = "Print all found primes")]
|
||||
verbose:bool,
|
||||
#[structopt(short, long, name = "FILE", help = "Import prime numbers from FILE")]
|
||||
import:Option<PathBuf>,
|
||||
#[structopt(short, long, help = "Test if n is prime instead of generation")]
|
||||
#[structopt(short, long, help = "Test if num is prime instead of generation")]
|
||||
test:bool,
|
||||
#[structopt(help = "Ordinal of the prime to generate")]
|
||||
n:u64,
|
||||
#[structopt(help = "Max of the prime to generate or number to test for primality")]
|
||||
num:u64,
|
||||
#[structopt(short, long, name = "n", default_value = "1", help = "Number of threads to spawn")]
|
||||
jobs:u64,
|
||||
}
|
||||
|
||||
fn main()
|
||||
{
|
||||
const SEGMENT_SIZE:usize = 0x40000000;
|
||||
|
||||
fn main() {
|
||||
let opts = Opt::from_args();
|
||||
|
||||
// get the first `n` primes
|
||||
let n = opts.n;
|
||||
let mut primes:VecDeque<u64> = VecDeque::new();
|
||||
let mut prime_list = Vec::<usize>::new();
|
||||
let mut arr = vec![false; SEGMENT_SIZE];
|
||||
|
||||
if opts.import.is_some()
|
||||
{
|
||||
if opts.import.is_some() {
|
||||
let in_file = File::open(opts.import.unwrap()).unwrap();
|
||||
let reader = BufReader::new(in_file);
|
||||
reader.lines().into_iter().for_each(|x| {
|
||||
primes.push_back(x.unwrap().parse().unwrap());
|
||||
});
|
||||
}
|
||||
|
||||
if opts.test
|
||||
{
|
||||
let mut res:bool;
|
||||
// if no primes were imported, test from beginning (2)
|
||||
if primes.len() == 0
|
||||
{
|
||||
res = test::is_prime(n);
|
||||
}
|
||||
// `n` should be in `primes` if the last prime is larger than `n`
|
||||
else if primes.back().unwrap() >= &(n)
|
||||
{
|
||||
res = primes.contains(&(n));
|
||||
}
|
||||
// we can memory test `n` if the last prime is >= sqrt(n)
|
||||
else if primes.back().unwrap() >= &((n as f64).sqrt() as u64)
|
||||
{
|
||||
res = test::is_prime_mem(n, &primes)
|
||||
}
|
||||
/*
|
||||
* if we have less primes than sqrt(n) then we can test all those
|
||||
* prior to the last prime in the list, and then begin testing odd
|
||||
* numbers.
|
||||
*/
|
||||
else
|
||||
{
|
||||
res = test::is_prime_mem(n, &primes);
|
||||
if res
|
||||
{
|
||||
res = test::is_prime_f(n, primes.back().unwrap() + 2);
|
||||
}
|
||||
}
|
||||
|
||||
if res
|
||||
{
|
||||
if opts.verbose
|
||||
{
|
||||
println!("true");
|
||||
}
|
||||
std::process::exit(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
if opts.verbose
|
||||
{
|
||||
println!("false");
|
||||
}
|
||||
std::process::exit(1);
|
||||
for p in reader.lines().into_iter() {
|
||||
prime_list.push(p.unwrap().parse().unwrap());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// if `primes` already contains the nth prime, print it
|
||||
if primes.len() >= n as usize
|
||||
{
|
||||
println!("{}", primes.get((n as usize) - 1).unwrap());
|
||||
|
||||
let limit = (opts.num * 5) as usize;
|
||||
if opts.num < 2 || limit > SEGMENT_SIZE {
|
||||
eprintln!("Invalid value for num: {}", opts.num);
|
||||
process::exit(1);
|
||||
}
|
||||
if opts.num > 2 {
|
||||
arr[2] = true;
|
||||
}
|
||||
if opts.num > 3 {
|
||||
arr[3] = true;
|
||||
}
|
||||
|
||||
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 = (xx3 + yy) as usize;
|
||||
if n2 % 12 == 7 {
|
||||
arr[n2] = !arr[n2];
|
||||
}
|
||||
|
||||
if x > y {
|
||||
let n3 = (xx3 - yy) as usize;
|
||||
if n3 % 12 == 11 {
|
||||
arr[n3] = !arr[n3];
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
let mut candidate:u64;
|
||||
}
|
||||
|
||||
if primes.len() == 0
|
||||
{
|
||||
// assume 2 as a prime
|
||||
primes.push_back(2);
|
||||
if opts.verbose
|
||||
{
|
||||
println!("{}", 2);
|
||||
}
|
||||
candidate = 3;
|
||||
}
|
||||
else if primes.len() == 1
|
||||
{
|
||||
candidate = 3;
|
||||
}
|
||||
else
|
||||
{
|
||||
candidate = *primes.back().unwrap() + 2;
|
||||
}
|
||||
for i in 5..=(f64::sqrt(opts.num as f64) as u64 + 1) as usize {
|
||||
if !arr[i] {
|
||||
continue;
|
||||
}
|
||||
|
||||
while primes.len() < n as usize
|
||||
{
|
||||
if test::is_prime_mem(candidate, &primes)
|
||||
{
|
||||
primes.push_back(candidate);
|
||||
if opts.verbose
|
||||
{
|
||||
println!("{}", candidate);
|
||||
}
|
||||
}
|
||||
let mut j = i * i;
|
||||
while j <= (opts.num as usize) {
|
||||
arr[j] = false;
|
||||
j += i * i;
|
||||
}
|
||||
}
|
||||
|
||||
candidate += 2;
|
||||
for i in 2..=(opts.num as usize) {
|
||||
if arr[i] {
|
||||
if opts.verbose && !opts.test {
|
||||
println!("{}", i);
|
||||
}
|
||||
prime_list.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
if !opts.verbose
|
||||
{
|
||||
println!("{}", primes.get((n as usize) - 1).unwrap());
|
||||
if !opts.verbose && !opts.test {
|
||||
println!("{}", prime_list.last().unwrap());
|
||||
} else if opts.test {
|
||||
if *prime_list.last().unwrap() == (opts.num as usize) {
|
||||
if opts.verbose {
|
||||
println!("{} is prime", opts.num);
|
||||
}
|
||||
process::exit(0);
|
||||
} else {
|
||||
if opts.verbose {
|
||||
println!("{} is composite", opts.num);
|
||||
}
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
68
src/test.rs
68
src/test.rs
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||
* Author: Ortega Froysa, Nicolás <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/>.
|
||||
*/
|
||||
|
||||
use std::collections::VecDeque;
|
||||
use rayon::prelude::*;
|
||||
|
||||
pub fn is_prime_f(n:u64, b:u64) -> bool
|
||||
{
|
||||
assert_ne!(b, 0);
|
||||
assert_ne!(b, 1);
|
||||
|
||||
if n == 1
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
let mut start = b;
|
||||
if start == 2
|
||||
{
|
||||
if n % 2 == 0
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
start += 1;
|
||||
}
|
||||
}
|
||||
// skip even numbers
|
||||
else if start % 2 == 0
|
||||
{
|
||||
start += 1;
|
||||
}
|
||||
|
||||
let limit = (n as f64).sqrt() as u64 + 1;
|
||||
let composite = (start..limit).step_by(2).collect::<Vec<u64>>()
|
||||
.par_iter().any(|x| n % x == 0);
|
||||
return !composite;
|
||||
}
|
||||
|
||||
pub fn is_prime(n:u64) -> bool
|
||||
{
|
||||
return is_prime_f(n, 2);
|
||||
}
|
||||
|
||||
pub fn is_prime_mem(n:u64, primes:&VecDeque<u64>) -> bool
|
||||
{
|
||||
let limit = (n as f64).sqrt() as u64;
|
||||
let pp = primes.partition_point(|x| *x < limit);
|
||||
//let composite = primes.par_iter().take(pp+1).any(|x| n % *x == 0);
|
||||
let composite = primes.iter().take(pp+1).any(|x| n % *x == 0);
|
||||
return !composite;
|
||||
}
|
||||
42
test.sh
42
test.sh
@@ -1,28 +1,50 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo -n "Prime finding..."
|
||||
if [[ $(./target/release/indivisible 5) == 11 ]]
|
||||
DEBUG_BIN="./target/debug/indivisible"
|
||||
RELEASE_BIN="./target/release/indivisible"
|
||||
|
||||
if [ -f "$DEBUG_BIN" ]
|
||||
then
|
||||
echo " pass"
|
||||
BINARY="$DEBUG_BIN"
|
||||
elif [ -f "$RELEASE_BIN" ]
|
||||
then
|
||||
BINARY="$RELEASE_BIN"
|
||||
else
|
||||
echo " FAIL"
|
||||
>&2 echo "No valid binary found. Please compile the project."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo -n "Positive prime test..."
|
||||
if ./target/release/indivisible -t 11
|
||||
tests=0
|
||||
passed=0
|
||||
|
||||
((tests++))
|
||||
echo -n "${tests}: Find 5th prime number..."
|
||||
if [[ $("$BINARY" 5) == 11 ]]
|
||||
then
|
||||
echo " pass"
|
||||
((passed++))
|
||||
else
|
||||
echo " FAIL"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo -n "Negative prime test..."
|
||||
if ! ./target/release/indivisible -t 9
|
||||
((tests++))
|
||||
echo -n "${tests}: 11 is prime..."
|
||||
if "$BINARY" -t 11
|
||||
then
|
||||
echo " pass"
|
||||
((passed++))
|
||||
else
|
||||
echo " FAIL"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
((tests++))
|
||||
echo -n "${tests}: 9 is not prime..."
|
||||
if ! "$BINARY" -t 9
|
||||
then
|
||||
echo " pass"
|
||||
((passed++))
|
||||
else
|
||||
echo " FAIL"
|
||||
fi
|
||||
|
||||
echo "Results: $passed/$tests"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/bin/sh
|
||||
PREFIX="/usr/local"
|
||||
PREFIX="${PREFIX:-/usr/local}"
|
||||
echo "Remove $PREFIX/bin/indivisible"
|
||||
rm -f $PREFIX/bin/indivisible
|
||||
rm -f "$PREFIX/bin/indivisible"
|
||||
echo "Remove $PREFIX/local/man/man1/indivisible.1.gz"
|
||||
rm -f $PREFIX/local/man/man1/indivisible.1.gz
|
||||
rm -f "$PREFIX/share/man/man1/indivisible.1.gz"
|
||||
|
||||
Reference in New Issue
Block a user