From 22c7702cf0370532ea8725995cd690fcd7414ef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20A=2E=20Ortega?= Date: Sun, 1 Jan 2017 17:05:25 +0100 Subject: [PATCH] We only need to test up to the sqrt(num). --- src/main.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main.c b/src/main.c index 70a6601..bee9db6 100644 --- a/src/main.c +++ b/src/main.c @@ -104,8 +104,8 @@ int main(int argc, char *argv[]) { mpz_init(num); // Variable for half `num' - mpz_t halfNum; - mpz_init(halfNum); + mpz_t numRoot; + mpz_init(numRoot); if(efile == NULL) puts("Use Ctrl+C to exit."); @@ -167,14 +167,14 @@ int main(int argc, char *argv[]) { } do { - // Calculate half of `num' - mpz_fdiv_q_ui(halfNum, num, 2); + // Calculate the sqrt(num) + mpz_sqrt(numRoot, num); /** * Loop through primes we've found until we get to half of the number * we're analyzing. Also, skip `2' since we're not testing even * numbers. */ - for(size_t i = 1; mpz_cmp(primes.list[i], halfNum) < 0; ++i) { + for(size_t i = 1; mpz_cmp(primes.list[i], numRoot) <= 0; ++i) { // If `num' is divisible by a prime then go to the next number if(mpz_divisible_p(num, primes.list[i]) != 0) goto nextNum; @@ -218,7 +218,7 @@ nextNum: releaseMemory: puts("Clearing memory..."); // Clear GMP variables - mpz_clear(halfNum); + mpz_clear(numRoot); mpz_clear(num); // Deinitialize the list deInitList(&primes);