According to http://ift.tt/1nzO08Q, if a^(n-1) does not equal 1 mod n, then the number is a composite number. As we know, 3 is a prime number while 9 is not. My Java skills are very outdated and I'm likely forgetting something very simple. Keep in mind this is only the beginning of the test as well, not the full implementation of the test. The example code below returns false for both numbers, while only 9 should return false.
import java.util.Random;
import java.util.Scanner;
public class LucasTest
{
private static int n;
private static boolean primeResult;
public static int randInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
public static boolean isPrime(int num)
{
int a = randInt(2, num - 1);
int b = num - 1;
double c = Math.pow(a,b);
if (c != (1.0 % num)) {
return false;
}
return true;
}
public static void main(String args[])
{
System.out.println("Enter an integer:");
Scanner pNum = new Scanner(System.in);
n = pNum.nextInt();
primeResult = isPrime(n);
System.out.println("Number is likely prime?: " + primeResult);
}
}
Aucun commentaire:
Enregistrer un commentaire