73 lines
1.7 KiB
C
Raw Normal View History

2018-10-18 12:04:07 +02:00
/*
2019-09-23 20:44:23 +02:00
* Copyright (C) 2019 Ortega Froysa, Nicolás <nicolas@ortegas.org>
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
2018-10-18 12:04:07 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2019-09-23 20:44:23 +02:00
#include "globals.h"
2019-09-24 08:20:09 +02:00
#include "llist.h"
2019-09-23 20:44:23 +02:00
#include <signal.h>
2018-10-18 12:04:07 +02:00
#include <stdio.h>
2018-10-18 14:56:15 +02:00
#include <gmp.h>
2018-10-18 12:04:07 +02:00
2019-09-23 20:44:23 +02:00
int run = 1;
2018-10-18 12:04:07 +02:00
2019-09-23 20:44:23 +02:00
void quit_signal(int signum) {
printf("Received signal %d. Ending process...\n", signum);
run = 0;
}
2018-10-18 12:04:07 +02:00
2019-09-23 20:44:23 +02:00
int main() {
printf("%s v%s\n", APP_NAME, VERSION);
2018-10-18 14:56:15 +02:00
2019-09-23 20:44:23 +02:00
signal(SIGINT, quit_signal);
2018-10-18 14:56:15 +02:00
2019-09-23 20:44:23 +02:00
mpz_t tnum; // number to be tested for prime-ness
mpz_t tsqrt; // square root of tnum
mpz_t aux; // auxilary number to test against tnum
mpz_t r; // remainder from modulus operation
2018-10-18 14:56:15 +02:00
2019-09-23 20:44:23 +02:00
mpz_inits(tnum, tsqrt, aux, r, NULL);
mpz_set_ui(tnum, 3);
mpz_sqrt(tsqrt, tnum);
2018-10-18 14:56:15 +02:00
2019-09-23 20:44:23 +02:00
while(run)
2018-10-18 14:56:15 +02:00
{
2019-09-23 20:44:23 +02:00
mpz_set_ui(aux, 3);
2018-10-18 14:56:15 +02:00
int is_prime = 1;
2019-09-23 20:44:23 +02:00
while(mpz_cmp(aux, tsqrt) <= 0)
2018-10-18 14:56:15 +02:00
{
2019-09-23 20:44:23 +02:00
mpz_mod(r, tnum, aux);
if(mpz_cmp_ui(r, 0) == 0)
2018-10-18 14:56:15 +02:00
is_prime = 0;
2019-09-23 20:44:23 +02:00
mpz_add_ui(aux, aux, 2);
2018-10-18 14:56:15 +02:00
}
2019-09-23 20:44:23 +02:00
2018-10-18 14:56:15 +02:00
if(is_prime)
2018-10-18 15:05:01 +02:00
{
2019-09-23 20:44:23 +02:00
mpz_out_str(stdout, 10, tnum);
printf("\n");
2018-10-18 15:05:01 +02:00
}
2019-09-23 20:44:23 +02:00
mpz_add_ui(tnum, tnum, 2);
mpz_sqrt(tsqrt, tnum);
2018-10-18 14:56:15 +02:00
}
2019-09-23 20:44:23 +02:00
mpz_clears(tnum, tsqrt, aux, r, NULL);
2018-10-18 14:56:15 +02:00
2018-10-18 12:04:07 +02:00
return 0;
}