change(generatePrimesCPU): Now aborting at half of candidate

Numbers larger than the half of a number are not able to divide a number
This commit is contained in:
datalore 2024-10-03 01:13:34 +02:00
parent 8325cddef8
commit ac1a8b7039

View File

@ -33,12 +33,18 @@ func printProgress(progress <-chan float32, message string) {
}
func checkIsDivisibleByPrime(number int64, offset, stride int, primes *[]int64, resultChannel chan bool, ctx context.Context) {
threshold := number / 2
for i := offset; i < len(*primes); i += stride {
select {
case <-ctx.Done():
resultChannel <- false
return
default:
// no need to continue checking if number is greater than half our number, division is already impossible
if (*primes)[i] > threshold {
resultChannel <- false
return
}
// only have to check primes, because all other numbers are multiple of primes.
// if a number is divisible by a multiple of a prime, it is by definition also divisible by the prime itself.
if number % (*primes)[i] == 0 {