Class Primes


  • public final class Primes
    extends java.lang.Object
    Methods related to prime numbers in the range of int.
    • primality test
    • prime number generation
    • factorization
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) static java.lang.String NUMBER_TOO_SMALL
      Exception message format when an argument is too small.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Primes()
      Utility class.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean isPrime​(int n)
      Primality test: tells if the argument is a (provable) prime or not.
      static int nextPrime​(int n)
      Return the smallest prime greater than or equal to n.
      static java.util.List<java.lang.Integer> primeFactors​(int n)
      Prime factors decomposition.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • NUMBER_TOO_SMALL

        static final java.lang.String NUMBER_TOO_SMALL
        Exception message format when an argument is too small.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Primes

        private Primes()
        Utility class.
    • Method Detail

      • isPrime

        public static boolean isPrime​(int n)
        Primality test: tells if the argument is a (provable) prime or not.

        It uses the Miller-Rabin probabilistic test in such a way that a result is guaranteed: it uses the firsts prime numbers as successive base (see Handbook of applied cryptography by Menezes, table 4.1).

        Parameters:
        n - Number to test.
        Returns:
        true if n is prime. All numbers < 2 return false.
      • nextPrime

        public static int nextPrime​(int n)
        Return the smallest prime greater than or equal to n.
        Parameters:
        n - Positive number.
        Returns:
        the smallest prime greater than or equal to n.
        Throws:
        java.lang.IllegalArgumentException - if n < 0.
      • primeFactors

        public static java.util.List<java.lang.Integer> primeFactors​(int n)
        Prime factors decomposition.
        Parameters:
        n - Number to factorize: must be ≥ 2.
        Returns:
        the list of prime factors of n.
        Throws:
        java.lang.IllegalArgumentException - if n < 2.