We only need to test up to the sqrt(num).
This commit is contained in:
parent
f8726497a4
commit
22c7702cf0
12
src/main.c
12
src/main.c
@ -104,8 +104,8 @@ int main(int argc, char *argv[]) {
|
|||||||
mpz_init(num);
|
mpz_init(num);
|
||||||
|
|
||||||
// Variable for half `num'
|
// Variable for half `num'
|
||||||
mpz_t halfNum;
|
mpz_t numRoot;
|
||||||
mpz_init(halfNum);
|
mpz_init(numRoot);
|
||||||
|
|
||||||
if(efile == NULL) puts("Use Ctrl+C to exit.");
|
if(efile == NULL) puts("Use Ctrl+C to exit.");
|
||||||
|
|
||||||
@ -167,14 +167,14 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
// Calculate half of `num'
|
// Calculate the sqrt(num)
|
||||||
mpz_fdiv_q_ui(halfNum, num, 2);
|
mpz_sqrt(numRoot, num);
|
||||||
/**
|
/**
|
||||||
* 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. Also, skip `2' since we're not testing even
|
* we're analyzing. Also, skip `2' since we're not testing even
|
||||||
* numbers.
|
* 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 `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;
|
||||||
@ -218,7 +218,7 @@ nextNum:
|
|||||||
releaseMemory:
|
releaseMemory:
|
||||||
puts("Clearing memory...");
|
puts("Clearing memory...");
|
||||||
// Clear GMP variables
|
// Clear GMP variables
|
||||||
mpz_clear(halfNum);
|
mpz_clear(numRoot);
|
||||||
mpz_clear(num);
|
mpz_clear(num);
|
||||||
// Deinitialize the list
|
// Deinitialize the list
|
||||||
deInitList(&primes);
|
deInitList(&primes);
|
||||||
|
Loading…
Reference in New Issue
Block a user