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.
This commit is contained in:
parent
3e902efd18
commit
f8726497a4
19
src/main.c
19
src/main.c
@ -116,12 +116,16 @@ int main(int argc, char *argv[]) {
|
|||||||
fprintf(stderr, "Failed to allocate more memory for list.\n");
|
fprintf(stderr, "Failed to allocate more memory for list.\n");
|
||||||
goto releaseMemory;
|
goto releaseMemory;
|
||||||
}
|
}
|
||||||
if(!f_quiet) {
|
// Add 3 as well to optimize the algorithm
|
||||||
if(mpz_out_str(stdout, base, num) == 0)
|
mpz_set_ui(num, 3);
|
||||||
fprintf(stderr, "Could not print to `stdout'!\n");
|
if(addToList(&primes, num) == 1) {
|
||||||
printf("\n");
|
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 {
|
} else {
|
||||||
// Load primes from file
|
// Load primes from file
|
||||||
int err = inputPrimes(file, &primes);
|
int err = inputPrimes(file, &primes);
|
||||||
@ -167,9 +171,10 @@ int main(int argc, char *argv[]) {
|
|||||||
mpz_fdiv_q_ui(halfNum, num, 2);
|
mpz_fdiv_q_ui(halfNum, num, 2);
|
||||||
/**
|
/**
|
||||||
* Loop through primes we've found until we get to half of the number
|
* 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 `num' is divisible by a prime then go to the next number
|
||||||
if(mpz_divisible_p(num, primes.list[i]) != 0)
|
if(mpz_divisible_p(num, primes.list[i]) != 0)
|
||||||
goto nextNum;
|
goto nextNum;
|
||||||
|
Loading…
Reference in New Issue
Block a user