Help needed with speading up a function.
| From: | William Bailey | Date: | Wed, 06 Mar 2002 15:32:24 +0000 |
| Subject: | Help needed with speading up a function. | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-87475@lists.php.net to get a copy of this message | ||
Hello again.
I have the following function that generates a prime number of x
bits. It seems to work but i am just trying to see if i can make it any
faster as generateing 1024 bit prime can take a while. so i thoought i
would ask here to see if anybody has any ideas or suggestions.
The function is as follows:
mt_srand((double)microtime()*10000);
function generate_prime ($bits) {
$number=gmp_init('0');
for($i=$bits; $i>=0; $i--){
$rand=mt_rand()%2;
gmp_setbit($number, $i, $rand);
}
while(gmp_prob_prime($number)<1){
$number=gmp_add($number, 1);
}
if(strlen(gmp_strval($number, 2))!=$bits){
$number=generate_prime($bits);
}else{
return (string)gmp_strval($number);
}
}
At the moment im generating a random number of the required length and
then +1ing it untill it is a prime. I suppose i really want to know if
their is some way of knowing how close you are to a possiable prime so
that if the random number is too far away then it could call itself again
and try a different random start location.
I look forward to any ideas that you might have.
Regards,
William.
--
William Bailey.
http://wb.pro-net.co.uk