6 Commits

Author SHA1 Message Date
nortega d7c73cbc91 Update README to include information about algorithm used. 2026-03-05 10:16:01 +01:00
nortega b96a0a80d2 Simplify sqrt() operation.
More readable.
2026-03-04 10:25:59 +01:00
nortega 6d55f0a10a benchmark: inform about configuration in output.
This is useful for default values.
2026-02-23 12:19:01 +01:00
nortega 0cc8e94224 Add test to find last prime before 10^9. 2025-12-11 08:06:37 +01:00
nortega 1348f4343a Document work_segment known_primes parameter. 2025-12-11 06:36:44 +01:00
nortega 83b5e4a561 Limit known-prime multiples marking until sqrt(end). 2025-12-11 06:34:42 +01:00
4 changed files with 34 additions and 4 deletions
+3 -1
View File
@@ -1,6 +1,8 @@
# Indivisible # Indivisible
Indivisible is an optimized prime number generator and tester. Indivisible is an optimized prime number generator and tester using a segmented
version of the [Sieve of
Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes).
## Build & Installation ## Build & Installation
+3
View File
@@ -21,6 +21,7 @@ do
case "$opt" in case "$opt" in
s) s)
OPTIONS=("${OPTIONS[@]}" -s "$OPTARG") OPTIONS=("${OPTIONS[@]}" -s "$OPTARG")
SIEVE=$OPTARG
;; ;;
t) t)
TRIALS="$OPTARG" TRIALS="$OPTARG"
@@ -33,6 +34,8 @@ do
done done
echo "Calculating primes up to 1,000,000,000" echo "Calculating primes up to 1,000,000,000"
echo "Trials: $TRIALS"
echo "Sieve segment size: ${SIEVE:-"default"}"
TOTAL="0" TOTAL="0"
for _ in $(seq "$TRIALS") for _ in $(seq "$TRIALS")
do do
+7
View File
@@ -19,6 +19,7 @@
/** /**
* @brief Work on a segment. * @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 start:usize Beginning of the segment (inclusive).
* @param end:usize End of the segment (exclusive). * @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 sieve = vec![true; end - start];
let mut found_primes = Vec::new(); let mut found_primes = Vec::new();
let sqrt_end = (end as f64).sqrt() as usize;
for p in known_primes { for p in known_primes {
let prime = *p as usize; let prime = *p as usize;
if prime > sqrt_end {
break;
}
let modu = start % prime; let modu = start % prime;
let mut mult = if modu == 0 { let mut mult = if modu == 0 {
start start
+21 -3
View File
@@ -17,7 +17,7 @@ fi
tests=0 tests=0
passed=0 passed=0
## TEST 1 ## TEST
((tests++)) ((tests++))
echo -n "${tests}: Find all prime numbers before 70..." 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 " 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" echo " FAIL"
fi 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++)) ((tests++))
echo -n "${tests}: 11 is prime..." echo -n "${tests}: 11 is prime..."
if "$BINARY" -t 11 if "$BINARY" -t 11
@@ -41,7 +54,7 @@ else
echo " FAIL" echo " FAIL"
fi fi
## TEST 3 ## TEST
((tests++)) ((tests++))
echo -n "${tests}: 9 is not prime..." echo -n "${tests}: 9 is not prime..."
if ! "$BINARY" -t 9 if ! "$BINARY" -t 9
@@ -54,3 +67,8 @@ fi
## RESULTS ## RESULTS
echo "Results: $passed/$tests" echo "Results: $passed/$tests"
if [ $passed -ne $tests ]
then
exit 1
fi