Ejemplo n.º 1
0
 /// Prime factors decomposition
 ///
 /// @param n number to factorize: must be ≥ 2
 /// @return list of prime factors of n
 /// @; 2.
 public static List <int> primeFactors(int n)
 {
     if (n < 2)
     {
         throw new ArgumentException();                 // MathIllegalArgumentException(LocalizedFormats.NUMBER_TOO_SMALL, n, 2);
     }
     // slower than trial div unless we do an awful lot of computation
     // (then it finally gets JIT-compiled efficiently
     // List<Integer> out = PollardRho.primeFactors(n);
     return(SmallPrimes.trialDivision(n));
 }
Ejemplo n.º 2
0
        /// Primality test: tells if the argument is a (provable) prime or not.
        /// <p>
        /// 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).
        ///
        /// @param n number to test.
        /// @return true if n is prime. (All numbers &lt; 2 return false).
        public static bool isPrime(int n)
        {
            if (n < 2)
            {
                return(false);
            }

            foreach (int p in SmallPrimes.PRIMES)
            {
                if (0 == (n % p))
                {
                    return(n == p);
                }
            }
            return(SmallPrimes.millerRabinPrimeTest(n));
        }