Compare commits
45 Commits
82d75227c4
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
| 0cc8e94224 | |||
| 1348f4343a | |||
| 83b5e4a561 | |||
| 084aa14026 | |||
| 966574dd1f | |||
| 41bc18a17c | |||
| 5dde94ae8c | |||
| be3fd52cfa | |||
| 0b391ed590 | |||
| f5b971e35f | |||
| abfb1aee89 | |||
| 4b3bf0cfac | |||
| ba3478e6ce | |||
| 5ee76502fa | |||
| 72d6f4e042 | |||
| d4dc54fe59 | |||
| c7f4f3c2c4 | |||
| 6c789195e6 | |||
| 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 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1,3 @@
|
|||||||
/target
|
/target
|
||||||
|
/indivisible.1.gz
|
||||||
|
callgrind.out.*
|
||||||
|
|||||||
78
Cargo.lock
generated
78
Cargo.lock
generated
@@ -4,9 +4,9 @@ version = 4
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ansi_term"
|
name = "ansi_term"
|
||||||
version = "0.11.0"
|
version = "0.12.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
|
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
@@ -24,15 +24,15 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "1.2.1"
|
version = "1.3.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
|
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap"
|
name = "clap"
|
||||||
version = "2.33.3"
|
version = "2.34.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
|
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ansi_term",
|
"ansi_term",
|
||||||
"atty",
|
"atty",
|
||||||
@@ -45,18 +45,18 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "heck"
|
name = "heck"
|
||||||
version = "0.3.2"
|
version = "0.3.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "87cbf45460356b7deeb5e3415b5563308c0a9b057c85e12b06ad551f98d0a6ac"
|
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"unicode-segmentation",
|
"unicode-segmentation",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hermit-abi"
|
name = "hermit-abi"
|
||||||
version = "0.1.18"
|
version = "0.1.19"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c"
|
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
@@ -70,15 +70,15 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lazy_static"
|
name = "lazy_static"
|
||||||
version = "1.4.0"
|
version = "1.5.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.87"
|
version = "0.2.178"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "265d751d31d6780a3f956bb5b8022feba2d94eeee5a84ba64f4212eedca42213"
|
checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro-error"
|
name = "proc-macro-error"
|
||||||
@@ -106,18 +106,18 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.24"
|
version = "1.0.103"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
|
checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"unicode-xid",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "quote"
|
name = "quote"
|
||||||
version = "1.0.9"
|
version = "1.0.42"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
|
checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
@@ -130,9 +130,9 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "structopt"
|
name = "structopt"
|
||||||
version = "0.3.21"
|
version = "0.3.26"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5277acd7ee46e63e5168a80734c9f6ee81b1367a7d8772a2d765df2a3705d28c"
|
checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
@@ -141,9 +141,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "structopt-derive"
|
name = "structopt-derive"
|
||||||
version = "0.4.14"
|
version = "0.4.18"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5ba9cdfda491b814720b6b06e0cac513d922fc407582032e8706e9f137976f90"
|
checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"heck",
|
"heck",
|
||||||
"proc-macro-error",
|
"proc-macro-error",
|
||||||
@@ -154,13 +154,13 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "1.0.60"
|
version = "1.0.109"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081"
|
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"unicode-xid",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -173,22 +173,22 @@ dependencies = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode-segmentation"
|
name = "unicode-ident"
|
||||||
version = "1.7.1"
|
version = "1.0.22"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
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]]
|
[[package]]
|
||||||
name = "unicode-width"
|
name = "unicode-width"
|
||||||
version = "0.1.8"
|
version = "0.1.14"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
|
checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "unicode-xid"
|
|
||||||
version = "0.2.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "vec_map"
|
name = "vec_map"
|
||||||
@@ -198,9 +198,9 @@ checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "version_check"
|
name = "version_check"
|
||||||
version = "0.9.2"
|
version = "0.9.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
|
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi"
|
name = "winapi"
|
||||||
|
|||||||
@@ -4,11 +4,9 @@ version = "2.0.0"
|
|||||||
authors = ["Nicolás Ortega Froysa <nicolas@ortegas.org>"]
|
authors = ["Nicolás Ortega Froysa <nicolas@ortegas.org>"]
|
||||||
description = "A prime number generator and tester"
|
description = "A prime number generator and tester"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
readme = "README"
|
readme = "README.md"
|
||||||
license = "GPL-3.0"
|
license = "GPL-3.0"
|
||||||
keywords = [ "primes", "math" ]
|
keywords = [ "primes", "math" ]
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
structopt = "0.3.13"
|
structopt = "0.3"
|
||||||
|
|||||||
44
README.md
44
README.md
@@ -1,45 +1,35 @@
|
|||||||
# Indivisible
|
# Indivisible
|
||||||
|
|
||||||
Indivisible is an optimized prime number generator and tester written in Rust.
|
Indivisible is an optimized prime number generator and tester.
|
||||||
|
|
||||||
## Build & Installation
|
## Build & Installation
|
||||||
|
|
||||||
To build the project you will require the Rust compiler and build
|
Build a binary release using the following command:
|
||||||
system, `cargo`. At which point you simply run `cargo build` in the root
|
|
||||||
directory of the project. To create an optimized release build append
|
|
||||||
the `--release` flag to the previous command.
|
|
||||||
|
|
||||||
Once a release build has been compiled, you may install Indivisible and
|
```console
|
||||||
the manpage documentation by running the `install` script. In a similar
|
$ cargo build -r
|
||||||
manner, you can run the `uninstall` script to remove the previously
|
```
|
||||||
installed binary and documentation.
|
|
||||||
|
|
||||||
## Usage
|
Once compiled, you can install it using the `install` script. By default this
|
||||||
|
will install to `/usr/local/{bin,share}`, but you can optionally change the
|
||||||
|
`PREFIX` used as follows:
|
||||||
|
|
||||||
The purpose of Indivisible is to find the nth prime and all the primes
|
```console
|
||||||
before it. The basic usage is `indivisible <n>` where `n` is the ordinal
|
$ PREFIX="~/.local" ./install
|
||||||
of the prime you'd like to find. To display all primes before `n`, you
|
```
|
||||||
can run verbose mode by using the `--verbose` or simply `-v` option.
|
|
||||||
|
|
||||||
Since Indivisible generates primes using previously computed primes, you
|
Additionally there is an `uninstall` script available. If you have used a custom
|
||||||
can also import prime numbers previously computed with the `--import` or
|
`PREFIX` this must also be specified to uninstall in the same manner. To see
|
||||||
`-i` option. To store already computed primes you are expected to use
|
where you've installed it you can always use the following command:
|
||||||
piping like any UNIX user would expect. Here is an example:
|
|
||||||
|
|
||||||
```bash
|
```console
|
||||||
# store first 100 primes in ./primes
|
$ which indivisible
|
||||||
indivisible -v 100 > ./primes
|
|
||||||
# appends next 400 primes
|
|
||||||
indivisible -i ./primes -v 500 >> ./primes
|
|
||||||
# display the 600th prime
|
|
||||||
indivisible -i ./primes 600
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Legacy
|
## Legacy
|
||||||
|
|
||||||
This project was originally written in C. It can be found in the
|
This project was originally written in C. It can be found in the
|
||||||
[indivisible-legacy repository](https://gitlab.com/naortega/Indivisible-legacy)
|
[indivisible-legacy repository](https://code.ortegas.org/nortega/indivisible-legacy).
|
||||||
on my GitLab.
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
45
benchmark.sh
Executable file
45
benchmark.sh
Executable file
@@ -0,0 +1,45 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
EXE="./target/release/indivisible"
|
||||||
|
OPTIONS=()
|
||||||
|
TRIALS=20
|
||||||
|
|
||||||
|
if ! [ -f "$EXE" ]
|
||||||
|
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
|
||||||
|
|
||||||
|
while getopts "t:s:" opt
|
||||||
|
do
|
||||||
|
case "$opt" in
|
||||||
|
s)
|
||||||
|
OPTIONS=("${OPTIONS[@]}" -s "$OPTARG")
|
||||||
|
;;
|
||||||
|
t)
|
||||||
|
TRIALS="$OPTARG"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
>&2 echo "Uknown option $opt"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "Calculating primes up to 1,000,000,000"
|
||||||
|
TOTAL="0"
|
||||||
|
for _ in $(seq "$TRIALS")
|
||||||
|
do
|
||||||
|
TIME=$(command time -f "%e" "$EXE" "${OPTIONS[@]}" 1000000000 2>&1 >/dev/null)
|
||||||
|
TOTAL=$(calc "$TOTAL + $TIME")
|
||||||
|
done
|
||||||
|
|
||||||
|
AVG=$(calc "$TOTAL / $TRIALS")
|
||||||
|
|
||||||
|
echo "Average time: ${AVG}s"
|
||||||
@@ -6,7 +6,9 @@ indivisible \- prime number generator and tester
|
|||||||
[\fIOPTIONS\fR] <\fIn\fR>
|
[\fIOPTIONS\fR] <\fIn\fR>
|
||||||
|
|
||||||
.SH "DESCRIPTION"
|
.SH "DESCRIPTION"
|
||||||
Indivisible is a program for working with prime numbers. Its default behaviour will attempt to find the \fInth\fR prime.
|
Indivisible is a program for working with prime numbers. It can either generate
|
||||||
|
primes up to a limit \fIn\fR or test to see if \fIn\fR is a prime. It makes use
|
||||||
|
of a segmented sieve of Eratosthenes.
|
||||||
|
|
||||||
.SH "OPTIONS"
|
.SH "OPTIONS"
|
||||||
.TP
|
.TP
|
||||||
@@ -19,6 +21,9 @@ Import prime numbers from \fIFILE\fR
|
|||||||
\fB\-t\fR, \fB\-\-test\fR
|
\fB\-t\fR, \fB\-\-test\fR
|
||||||
Test if n is a prime instead of generation
|
Test if n is a prime instead of generation
|
||||||
.TP
|
.TP
|
||||||
|
\fB\-s\fR, \fB\-\-sieve\fR <\fISIZE\fR>
|
||||||
|
Set a custom sieve size
|
||||||
|
.TP
|
||||||
\fB\-v\fR, \fB\-\-verbose\fR
|
\fB\-v\fR, \fB\-\-verbose\fR
|
||||||
Print all found primes
|
Print all found primes
|
||||||
.TP
|
.TP
|
||||||
|
|||||||
9
install
9
install
@@ -6,11 +6,12 @@ then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
PREFIX="/usr/local"
|
PREFIX="${PREFIX:-/usr/local}"
|
||||||
|
|
||||||
echo "Installing ./target/release/indivisible to $PREFIX/bin/"
|
echo "Installing ./target/release/indivisible to $PREFIX/bin/"
|
||||||
install -m 755 ./target/release/indivisible $PREFIX/bin/
|
install -Dm755 ./target/release/indivisible "$PREFIX/bin/indivisible"
|
||||||
gzip -c indivisible.1 > indivisible.1.gz
|
gzip -k indivisible.1
|
||||||
echo "Installing manpage to $PREFIX/share/man/man1/"
|
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
|
rm indivisible.1.gz
|
||||||
|
|||||||
@@ -1,62 +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: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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
79
src/main.rs
79
src/main.rs
@@ -16,17 +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::cell::RefCell;
|
|
||||||
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 std::rc::Rc;
|
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
|
||||||
mod candidate;
|
|
||||||
use candidate::CandidateGenerator;
|
|
||||||
mod worker;
|
mod worker;
|
||||||
|
|
||||||
#[derive(StructOpt)]
|
#[derive(StructOpt)]
|
||||||
@@ -38,50 +33,72 @@ struct Opt {
|
|||||||
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", help = "Number of threads to spawn")]
|
#[structopt(short, long, name = "SIZE", default_value = "10485760", help = "Set a custom sieve size")]
|
||||||
jobs:Option<u64>,
|
sieve:usize,
|
||||||
|
//#[structopt(short, long, name = "n", default_value = "1", help = "Number of threads to spawn")]
|
||||||
|
//jobs:u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let opts = Opt::from_args();
|
let opts = Opt::from_args();
|
||||||
|
let mut prime_list = Vec::new();
|
||||||
let mut primes_list = Rc::new(RefCell::new(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() {
|
||||||
primes_list.borrow_mut().push_back(p.unwrap().parse().unwrap());
|
let prime:u64 = p.unwrap().parse().unwrap();
|
||||||
|
if (prime as usize) > opts.num {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
prime_list.push(prime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let jobs = match opts.jobs {
|
if opts.num < 2 {
|
||||||
Some(n) => n,
|
|
||||||
None => 1, // TODO: use number of CPUs
|
|
||||||
};
|
|
||||||
|
|
||||||
if opts.num == 0 {
|
|
||||||
eprintln!("Invalid value for num: {}", opts.num);
|
eprintln!("Invalid value for num: {}", opts.num);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.test && *primes_list.borrow().back().unwrap_or(&0) >= opts.num {
|
let mut start:usize = if prime_list.is_empty() {
|
||||||
for i in primes_list.borrow().iter() {
|
2
|
||||||
if *i == opts.num {
|
|
||||||
process::exit(0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
process::exit(1)
|
|
||||||
} else if !opts.test && primes_list.borrow().len() >= opts.num as usize {
|
|
||||||
let res = *primes_list.borrow().get(opts.num as usize).unwrap();
|
|
||||||
println!("{}", res);
|
|
||||||
} else {
|
} else {
|
||||||
let mut cand_gen = CandidateGenerator::new();
|
(*prime_list.last().unwrap() + 1) as usize
|
||||||
|
};
|
||||||
|
while start < opts.num {
|
||||||
|
let end = if start + opts.sieve < opts.num {
|
||||||
|
start + opts.sieve
|
||||||
|
} else {
|
||||||
|
opts.num + 1
|
||||||
|
};
|
||||||
|
let mut new_primes = worker::work_segment(&prime_list, start, end);
|
||||||
|
|
||||||
while primes_list.borrow().len() < opts.num as usize {
|
if opts.verbose {
|
||||||
let cand = cand_gen.next();
|
for p in &new_primes {
|
||||||
|
println!("{}", *p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
prime_list.append(&mut new_primes);
|
||||||
|
|
||||||
|
start += opts.sieve;
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts.test {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
} else if !opts.verbose {
|
||||||
|
println!("{}", prime_list.last().unwrap());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,21 +16,51 @@
|
|||||||
* 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 known_primes:&Vec<u64> List of known primes at least until sqrt(end).
|
||||||
|
* @param start:usize Beginning of the segment (inclusive).
|
||||||
|
* @param end:usize End of the segment (exclusive).
|
||||||
|
*
|
||||||
|
* @return List of primes found in segment.
|
||||||
|
*/
|
||||||
|
pub fn work_segment(known_primes:&Vec<u64>, start:usize, end:usize) -> Vec<u64> {
|
||||||
|
let mut sieve = vec![true; end - start];
|
||||||
|
let mut found_primes = Vec::new();
|
||||||
|
|
||||||
pub struct Worker {
|
let sqrt_end = f64::sqrt(end as f64) as usize;
|
||||||
primes_list:Rc<RefCell<VecDeque<u64>>>,
|
|
||||||
|
for p in known_primes {
|
||||||
|
let prime = *p as usize;
|
||||||
|
if prime > sqrt_end {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Worker {
|
let modu = start % prime;
|
||||||
pub fn new(primes_list:Rc<RefCell<VecDeque<u64>>>) -> Worker {
|
let mut mult = if modu == 0 {
|
||||||
Worker {
|
start
|
||||||
primes_list,
|
} else {
|
||||||
|
start + prime - modu
|
||||||
|
};
|
||||||
|
while mult < end {
|
||||||
|
sieve[mult - start] = false;
|
||||||
|
mult += prime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(&mut self) {
|
for i in 0..(end - start) {
|
||||||
|
if sieve[i] {
|
||||||
|
let prime = i + start;
|
||||||
|
found_primes.push(prime as u64);
|
||||||
|
|
||||||
|
let mut mult = prime * prime;
|
||||||
|
while mult < end {
|
||||||
|
sieve[mult - start] = false;
|
||||||
|
mult += prime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
found_primes
|
||||||
|
}
|
||||||
|
|||||||
64
test.sh
64
test.sh
@@ -1,28 +1,74 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
echo -n "Prime finding..."
|
DEBUG_BIN="./target/debug/indivisible"
|
||||||
if [[ $(./target/release/indivisible 5) == 11 ]]
|
RELEASE_BIN="./target/release/indivisible"
|
||||||
|
|
||||||
|
if [ -f "$DEBUG_BIN" ]
|
||||||
then
|
then
|
||||||
echo " pass"
|
BINARY="$DEBUG_BIN"
|
||||||
|
elif [ -f "$RELEASE_BIN" ]
|
||||||
|
then
|
||||||
|
BINARY="$RELEASE_BIN"
|
||||||
else
|
else
|
||||||
echo " FAIL"
|
>&2 echo "No valid binary found. Please compile the project."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo -n "Positive prime test..."
|
tests=0
|
||||||
if ./target/release/indivisible -t 11
|
passed=0
|
||||||
|
|
||||||
|
## TEST
|
||||||
|
((tests++))
|
||||||
|
echo -n "${tests}: Find all prime numbers before 70..."
|
||||||
|
expect="2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 "
|
||||||
|
|
||||||
|
if [ "$("$BINARY" -v 70 | tr '\n' ' ')" = "$expect" ]
|
||||||
then
|
then
|
||||||
echo " pass"
|
echo " pass"
|
||||||
|
((passed++))
|
||||||
else
|
else
|
||||||
echo " FAIL"
|
echo " FAIL"
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo -n "Negative prime test..."
|
## TEST
|
||||||
if ! ./target/release/indivisible -t 9
|
((tests++))
|
||||||
|
echo -n "${tests}: Find last prime before 1,000,000,000..."
|
||||||
|
expect="999999937"
|
||||||
|
|
||||||
|
if [ "$("$BINARY" 1000000000)" = "$expect" ]
|
||||||
then
|
then
|
||||||
echo " pass"
|
echo " pass"
|
||||||
|
((passed++))
|
||||||
else
|
else
|
||||||
echo " FAIL"
|
echo " FAIL"
|
||||||
|
fi
|
||||||
|
|
||||||
|
## TEST
|
||||||
|
((tests++))
|
||||||
|
echo -n "${tests}: 11 is prime..."
|
||||||
|
if "$BINARY" -t 11
|
||||||
|
then
|
||||||
|
echo " pass"
|
||||||
|
((passed++))
|
||||||
|
else
|
||||||
|
echo " FAIL"
|
||||||
|
fi
|
||||||
|
|
||||||
|
## TEST
|
||||||
|
((tests++))
|
||||||
|
echo -n "${tests}: 9 is not prime..."
|
||||||
|
if ! "$BINARY" -t 9
|
||||||
|
then
|
||||||
|
echo " pass"
|
||||||
|
((passed++))
|
||||||
|
else
|
||||||
|
echo " FAIL"
|
||||||
|
fi
|
||||||
|
|
||||||
|
## RESULTS
|
||||||
|
echo "Results: $passed/$tests"
|
||||||
|
|
||||||
|
if [ $passed -ne $tests ]
|
||||||
|
then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
PREFIX="/usr/local"
|
PREFIX="${PREFIX:-/usr/local}"
|
||||||
echo "Remove $PREFIX/bin/indivisible"
|
echo "Remove $PREFIX/bin/indivisible"
|
||||||
rm -f $PREFIX/bin/indivisible
|
rm -f "$PREFIX/bin/indivisible"
|
||||||
echo "Remove $PREFIX/local/man/man1/indivisible.1.gz"
|
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