gmp素数
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include <time.h>
int newprime(mpz_t p,mpz_t q){
mpz_nextprime(q, p);
return 1;
}
int main()
{
mpz_t bign;
mpz_init(bign);
mpz_init_set_str(bign, "0", 10);
mpz_t bign1;
mpz_init(bign1);
mpz_init_set_str(bign1, "0", 10);
unsigned long seed;
gmp_randstate_t rstate;
seed = time(NULL);
gmp_randinit_default(rstate);
gmp_randseed_ui(rstate, seed);
int rep = 0;
while(rep==0){
mpz_urandomb(bign,rstate,16);//0-2^16的随机数
rep=mpz_probab_prime_p(bign,10);
}
//gmp_printf("%Zd\n", bign);
int i;
srand((unsigned)time(NULL));
i = rand() % 5;
mpz_nextprime(bign1, bign);
if(i){
newprime(bign1,bign1);
i--;
}
gmp_printf("%Zd\n", bign1);
mpz_clear(bign);
mpz_clear(bign1);
}
编译方法:
gcc prime.c -o prime -lgmp