greeter

the best prime number checker

Jan 7th, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <gmp.h>
  5.  
  6. //GLOBAL VARIABLES
  7. mpz_t number, divisor, starter, counter, final, calcs;
  8.  
  9. void graceful_exit(int value) {
  10.     mpz_clear (number);
  11.     mpz_clear (final);
  12.     mpz_clear (divisor);
  13.     mpz_clear (starter);
  14.     mpz_clear (counter);
  15.     mpz_clear (calcs);
  16.     exit (value);
  17. }
  18.  
  19. int main(int argc, char *argv[]) {
  20.     int flag = 0, check = 0;
  21.     time_t start, end;
  22.     if (argc != 2) {
  23.         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]);
  24.         return 1;
  25.     }
  26.     mpz_init (number);
  27.     mpz_init (divisor);
  28.     mpz_init (starter);
  29.     mpz_init (counter);
  30.     mpz_init (final);
  31.     mpz_init (calcs);
  32.     mpz_set_ui (calcs, 1);
  33.     mpz_set_str (number, argv[1], 10);
  34.     flag = mpz_cmp_si(number, 2);
  35.     if (flag < 1) {
  36.         printf("Error: Number must be greater than 2. Please run the program and try again.\n");
  37.         graceful_exit(1);
  38.     }
  39.     flag = 0;
  40.     mpz_sqrt (counter, number);
  41.     mpz_set_ui (starter, 2);
  42.     gmp_printf("Checking %Zd for primality.\nTesting divisibility with all numbers up to %Zd\n\nPlease wait...\n", number, counter);
  43.     start = time(NULL);
  44.     mpz_mod (divisor, number, starter);
  45.     flag = mpz_cmp_si(divisor, 0);
  46.     if (flag == 0) {
  47.         end = time(NULL);
  48.         gmp_printf("%Zd is divisible by %Zd.\n\nCalculation took approximately %ld seconds\n", number, divisor, (end - start));
  49.         graceful_exit(0);
  50.     }
  51.     mpz_set_ui (starter, 5);
  52.     mpz_mod (divisor, number, starter);
  53.     flag = mpz_cmp_si(divisor, 0);
  54.     if (flag == 0) {
  55.         end = time(NULL);
  56.         gmp_printf("%Zd is divisible by %Zd.\n\nCalculation took approximately %ld seconds\n", number, divisor, (end - start));
  57.         graceful_exit(0);
  58.     }
  59.     do {
  60.         mpz_add_ui (final, final, 2);
  61.         mpz_mod (divisor, number, starter);
  62.         flag = mpz_cmp_si(divisor, 0);
  63.         if (flag == 0) {
  64.             end = time(NULL);
  65.             gmp_printf("%Zd is divisible by %Zd.\n\nCalculation took approximately %ld seconds\n", number, divisor, (end - start));
  66.             graceful_exit(0);
  67.         }
  68.         /*else {
  69.             mpz_add_ui (starter, starter, 2);
  70.             if (mpz_divisible_ui_p (final, 10000000) != 0) {
  71.                 gmp_printf("%Zd0 million calculations and counting...\n", calcs);
  72.                 mpz_add_ui (calcs, calcs, 1);
  73.             }*/
  74.             continue;
  75.     } while (mpz_cmp(final, counter) < 0);
  76.     end = time(NULL);
  77.     gmp_printf("%Zd is prime.\n\nCalculation took approximately %ld seconds\n", number, (end - start));
  78.     graceful_exit(0);
  79. }
  80.  
  81. //This prime number checker should be able to check any number for primality, so long as your machine
  82. //has enough memory and processing power to check it. All known bugs have been fixed. If you discover
  83. //any issues, please send an e-mail to ajr dot moore at gmail dot com detailing what exactly went wrong
  84. //Compile on linux with this command: gcc file.c -o file -lgmp
Advertisement
Add Comment
Please, Sign In to add comment