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 {