Only test up to the sqrt.

This commit is contained in:
Nicolás A. Ortega Froysa 2018-10-18 23:04:12 +02:00 committed by Nicolás Ortega Froysa
parent d2e037e357
commit 874cc75431
No known key found for this signature in database
GPG Key ID: FEC70E3BAE2E69BF

View File

@ -56,7 +56,11 @@ int main(int argc, char *argv[]) {
{ {
struct llist_item *item = prime_list.first; struct llist_item *item = prime_list.first;
int is_prime = 1; int is_prime = 1;
while(item)
mpz_t root;
mpz_init(root);
mpz_sqrt(root, aux);
while(item && mpz_cmp(item->num, root) < 0)
{ {
if(mpz_divisible_p(aux, item->num)) if(mpz_divisible_p(aux, item->num))
{ {
@ -79,6 +83,7 @@ int main(int argc, char *argv[]) {
printf("The %zu prime is ", prime_list.size); printf("The %zu prime is ", prime_list.size);
mpz_out_str(stdout, 10, prime_list.last->num); mpz_out_str(stdout, 10, prime_list.last->num);
printf("\n");
llist_deinit(&prime_list); llist_deinit(&prime_list);
return 0; return 0;