Implementation of Fermat's primality test

后端 未结 2 637
醉梦人生
醉梦人生 2021-01-14 21:54

Who wants to help me with my homework?

I\'m try to implement Fermat\'s primality test in Java using BigIntegers. My implementation is as follows, but unfortunately i

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-14 22:17

    Your use of the particular BigInteger constructor is reasonable, but you should use a rejection method to select a fermat base a. Here is a slight modification of your method in a class which also uses exactly one Random object:

    import java.math.BigInteger;
    import java.util.Random;
    
    public class FermatTestExample
    {
    
        private final static Random rand = new Random();
    
        private static BigInteger getRandomFermatBase(BigInteger n)
        {
            // Rejection method: ask for a random integer but reject it if it isn't
            // in the acceptable set.
    
            while (true)
            {
                final BigInteger a = new BigInteger (n.bitLength(), rand);
                // must have 1 <= a < n
                if (BigInteger.ONE.compareTo(a) <= 0 && a.compareTo(n) < 0)
                {
                    return a;
                }
            }
        }
    
        public static boolean checkPrime(BigInteger n, int maxIterations)
        {
            if (n.equals(BigInteger.ONE))
                return false;
    
            for (int i = 0; i < maxIterations; i++)
            {
                BigInteger a = getRandomFermatBase(n);
                a = a.modPow(n.subtract(BigInteger.ONE), n);
    
                if (!a.equals(BigInteger.ONE))
                    return false;
            }
    
            return true;
        }
    
        public static void main(String[] args)
        {
            System.out.printf("checkprime(2) is %b%n", checkPrime(BigInteger.valueOf(2L), 20));
            System.out.printf("checkprime(5) is %b%n", checkPrime(BigInteger.valueOf(5L), 20));
            System.out.printf("checkprime(7) is %b%n", checkPrime(BigInteger.valueOf(7L), 20));
            System.out.printf("checkprime(9) is %b%n", checkPrime(BigInteger.valueOf(9L), 20));
        }
    }
    

提交回复
热议问题