vendredi 2 janvier 2015

How do I get a N digit random prime number?

What am doing wrong here in this piece of code ? What is a better way to generate N digit random number ?



public static boolean isPrime(long n){
return (Math.pow(2, n-1) % n == 1) && (Math.pow(3, n-1) % n == 1);
}

public static long getPrime(int digit){
long sample;
long multiple = Math.round(Math.pow(10, digit));
do{
sample = Math.round(Math.random()* multiple);
//System.out.println(sample);
}while(!isPrime(sample));
return sample;
}




Aucun commentaire:

Enregistrer un commentaire