Compare commits

..

7 Commits

Author SHA1 Message Date
0cc8e94224 Add test to find last prime before 10^9. 2025-12-11 08:06:37 +01:00
1348f4343a Document work_segment known_primes parameter. 2025-12-11 06:36:44 +01:00
83b5e4a561 Limit known-prime multiples marking until sqrt(end). 2025-12-11 06:34:42 +01:00
084aa14026 Simplify README.md 2025-12-10 15:06:27 +01:00
966574dd1f Change default sieve size to 10485760.
I don't know why yet, but this seems to be the near optimal sieve size
when testing with the benchmarking script. It's equal to `1024^2 * 10`.
2025-12-10 14:59:52 +01:00
41bc18a17c Change test to 1,000,000,000 primes. 2025-12-10 14:59:33 +01:00
5dde94ae8c Add custom sieve and trials arguments to benchmark script. 2025-12-10 14:45:59 +01:00
5 changed files with 66 additions and 33 deletions

View File

@@ -1,38 +1,29 @@
# Indivisible
Indivisible is an optimized prime number generator and tester written in Rust.
Indivisible is an optimized prime number generator and tester.
## Build & Installation
To build the project you will require the Rust compiler and build
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.
Build a binary release using the following command:
Once a release build has been compiled, you may install Indivisible and
the manpage documentation by running the `install` script. In a similar
manner, you can run the `uninstall` script to remove the previously
installed binary and documentation.
```console
$ cargo build -r
```
## 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
before it. The basic usage is `indivisible <n>` where `n` is the ordinal
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.
```console
$ PREFIX="~/.local" ./install
```
Since Indivisible generates primes using previously computed primes, you
can also import prime numbers previously computed with the `--import` or
`-i` option. To store already computed primes you are expected to use
piping like any UNIX user would expect. Here is an example:
Additionally there is an `uninstall` script available. If you have used a custom
`PREFIX` this must also be specified to uninstall in the same manner. To see
where you've installed it you can always use the following command:
```bash
# store first 100 primes in ./primes
indivisible -v 100 > ./primes
# appends next 400 primes
indivisible -i ./primes -v 500 >> ./primes
# display the 600th prime
indivisible -i ./primes 600
```console
$ which indivisible
```
## Legacy

View File

@@ -1,9 +1,10 @@
#!/bin/bash
BIN="./target/release/indivisible"
EXE="./target/release/indivisible"
OPTIONS=()
TRIALS=20
if ! [ -f "$BIN" ]
if ! [ -f "$EXE" ]
then
>&2 echo "Release build not available. Please run 'cargo build -r'."
exit 1
@@ -15,11 +16,27 @@ then
exit 1
fi
echo "Calculating primes up to 100,000,000"
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" "$BIN" 100000000 2>&1 >/dev/null)
TIME=$(command time -f "%e" "$EXE" "${OPTIONS[@]}" 1000000000 2>&1 >/dev/null)
TOTAL=$(calc "$TOTAL + $TIME")
done

View File

@@ -35,7 +35,7 @@ struct Opt {
test:bool,
#[structopt(help = "Max of the prime to generate or number to test for primality")]
num:usize,
#[structopt(short, long, name = "SIZE", default_value = "1000", help = "Set a custom sieve size")]
#[structopt(short, long, name = "SIZE", default_value = "10485760", help = "Set a custom sieve size")]
sieve:usize,
//#[structopt(short, long, name = "n", default_value = "1", help = "Number of threads to spawn")]
//jobs:u64,

View File

@@ -19,6 +19,7 @@
/**
* @brief Work on a segment.
*
* @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).
*
@@ -28,8 +29,14 @@ 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();
let sqrt_end = f64::sqrt(end as f64) as usize;
for p in known_primes {
let prime = *p as usize;
if prime > sqrt_end {
break;
}
let modu = start % prime;
let mut mult = if modu == 0 {
start

24
test.sh
View File

@@ -17,7 +17,7 @@ fi
tests=0
passed=0
## TEST 1
## 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 "
@@ -30,7 +30,20 @@ else
echo " FAIL"
fi
## TEST 2
## TEST
((tests++))
echo -n "${tests}: Find last prime before 1,000,000,000..."
expect="999999937"
if [ "$("$BINARY" 1000000000)" = "$expect" ]
then
echo " pass"
((passed++))
else
echo " FAIL"
fi
## TEST
((tests++))
echo -n "${tests}: 11 is prime..."
if "$BINARY" -t 11
@@ -41,7 +54,7 @@ else
echo " FAIL"
fi
## TEST 3
## TEST
((tests++))
echo -n "${tests}: 9 is not prime..."
if ! "$BINARY" -t 9
@@ -54,3 +67,8 @@ fi
## RESULTS
echo "Results: $passed/$tests"
if [ $passed -ne $tests ]
then
exit 1
fi