Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <gmp.h>
- //GLOBAL VARIABLES
- mpz_t number, divisor, starter, counter, final, calcs;
- void graceful_exit(int value) {
- mpz_clear (number);
- mpz_clear (final);
- mpz_clear (divisor);
- mpz_clear (starter);
- mpz_clear (counter);
- mpz_clear (calcs);
- exit (value);
- }
- int main(int argc, char *argv[]) {
- int flag = 0, check = 0;
- time_t start, end;
- if (argc != 2) {
- printf("Usage: %s [number]\n\nYou must enter a number higher than two for this program to work properly.\nNOTE: This program will not check if your computer has enough memory to make the calculations.\n", argv[0]);
- return 1;
- }
- mpz_init (number);
- mpz_init (divisor);
- mpz_init (starter);
- mpz_init (counter);
- mpz_init (final);
- mpz_init (calcs);
- mpz_set_ui (calcs, 1);
- mpz_set_str (number, argv[1], 10);
- flag = mpz_cmp_si(number, 2);
- if (flag < 1) {
- printf("Error: Number must be greater than 2. Please run the program and try again.\n");
- graceful_exit(1);
- }
- flag = 0;
- mpz_sqrt (counter, number);
- mpz_set_ui (starter, 2);
- gmp_printf("Checking %Zd for primality.\nTesting divisibility with all numbers up to %Zd\n\nPlease wait...\n", number, counter);
- start = time(NULL);
- mpz_mod (divisor, number, starter);
- flag = mpz_cmp_si(divisor, 0);
- if (flag == 0) {
- end = time(NULL);
- gmp_printf("%Zd is divisible by %Zd.\n\nCalculation took approximately %ld seconds\n", number, divisor, (end - start));
- graceful_exit(0);
- }
- mpz_set_ui (starter, 5);
- mpz_mod (divisor, number, starter);
- flag = mpz_cmp_si(divisor, 0);
- if (flag == 0) {
- end = time(NULL);
- gmp_printf("%Zd is divisible by %Zd.\n\nCalculation took approximately %ld seconds\n", number, divisor, (end - start));
- graceful_exit(0);
- }
- do {
- mpz_add_ui (final, final, 2);
- mpz_mod (divisor, number, starter);
- flag = mpz_cmp_si(divisor, 0);
- if (flag == 0) {
- end = time(NULL);
- gmp_printf("%Zd is divisible by %Zd.\n\nCalculation took approximately %ld seconds\n", number, divisor, (end - start));
- graceful_exit(0);
- }
- /*else {
- mpz_add_ui (starter, starter, 2);
- if (mpz_divisible_ui_p (final, 10000000) != 0) {
- gmp_printf("%Zd0 million calculations and counting...\n", calcs);
- mpz_add_ui (calcs, calcs, 1);
- }*/
- continue;
- } while (mpz_cmp(final, counter) < 0);
- end = time(NULL);
- gmp_printf("%Zd is prime.\n\nCalculation took approximately %ld seconds\n", number, (end - start));
- graceful_exit(0);
- }
- //This prime number checker should be able to check any number for primality, so long as your machine
- //has enough memory and processing power to check it. All known bugs have been fixed. If you discover
- //any issues, please send an e-mail to ajr dot moore at gmail dot com detailing what exactly went wrong
- //Compile on linux with this command: gcc file.c -o file -lgmp
Advertisement
Add Comment
Please, Sign In to add comment