From f8726497a4676082bca126df7d8be48ca927abdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20A=2E=20Ortega?= Date: Fri, 30 Dec 2016 17:57:28 +0100 Subject: [PATCH] Don't test for divisibility by 2. We skip all even numbers, so we can skip 2, this also means giving 3 as a given. --- src/main.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main.c b/src/main.c index 0237d87..70a6601 100644 --- a/src/main.c +++ b/src/main.c @@ -116,12 +116,16 @@ int main(int argc, char *argv[]) { fprintf(stderr, "Failed to allocate more memory for list.\n"); goto releaseMemory; } - if(!f_quiet) { - if(mpz_out_str(stdout, base, num) == 0) - fprintf(stderr, "Could not print to `stdout'!\n"); - printf("\n"); + // Add 3 as well to optimize the algorithm + mpz_set_ui(num, 3); + if(addToList(&primes, num) == 1) { + fprintf(stderr, "Failed to allocate more memory for list.\n"); + goto releaseMemory; } - mpz_add_ui(num, num, 1); + if(!f_quiet) { + puts("2\n3"); + } + mpz_add_ui(num, num, 2); } else { // Load primes from file int err = inputPrimes(file, &primes); @@ -167,9 +171,10 @@ int main(int argc, char *argv[]) { mpz_fdiv_q_ui(halfNum, num, 2); /** * Loop through primes we've found until we get to half of the number - * we're analyzing + * we're analyzing. Also, skip `2' since we're not testing even + * numbers. */ - for(size_t i = 0; mpz_cmp(primes.list[i], halfNum) < 0; ++i) { + for(size_t i = 1; mpz_cmp(primes.list[i], halfNum) < 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;