Advertisement
Madmouse

roflmao, works great, until you start calculating large numb

Oct 24th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.44 KB | None | 0 0
  1. bool prime(mpz_t x)
  2. {
  3.     mpz_t i, f, sf;
  4.     mpz_init_set_ui(i, 3);
  5.     mpz_init(f);
  6.     mpz_init(sf);
  7.    
  8.     if(mpz_cmp_ui(x,2) == 0) return true;
  9.    
  10.     mpz_mod_ui(f, x, 2);
  11.     if(mpz_cmp_ui(x,2) < 0 || mpz_cmp_ui(f,0) == 0) return false;
  12.    
  13.     mpz_sqrt(sf,x);
  14.     for(i;mpz_cmp(i,sf) < 0 || mpz_cmp(i,sf) == 0;mpz_add_ui(i,i,1))
  15.     {
  16.         mpz_mod(f, x, i);
  17.         if(mpz_cmp_ui(f,0) == 0) return false;
  18.     }
  19.     mpz_clear(i);
  20.     mpz_clear(f);
  21.     mpz_clear(sf);
  22.     return true;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement