From ac1a8b70398842c8aa6b54246873d08aff1ba497 Mon Sep 17 00:00:00 2001 From: datalore Date: Thu, 3 Oct 2024 01:13:34 +0200 Subject: [PATCH] change(generatePrimesCPU): Now aborting at half of candidate Numbers larger than the half of a number are not able to divide a number --- main.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/main.go b/main.go index 58f4999..4bb4cf8 100644 --- a/main.go +++ b/main.go @@ -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 {