#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define TRUE 1
#define FALSE 0
/*
Name: 获取一个素数
Copyright:
Author: @dujianjian
Date: 01/11/12 20:02
Description:
*/
bool is_prime(long p)
{
long x = (long)sqrt(p);
long i = 3;
if(p==2)return TRUE;
if(p%2 == 0)return FALSE;
while(i<x+1){
if(p%i == 0)return FALSE;
i+=2;
}
return TRUE;
}
long get_prime(){
long a=0;
while(1){
srand(rand()+time(NULL));
a = rand();
if(is_prime(a))
{
printf("%d \n",a);
return a;
}
}
}
int main(){
long p,q;
p = get_prime();
q = get_prime();
printf("p = %ld,q = %ld\n",p,q);
printf("p = %d,q = %d\n",is_prime(p),is_prime(q));
getchar();
return 0;
}
示例结果: