Exemple #1
0
        public void TestPrimes()
        {
            // Test the first 1000 or so primes and not-primes
            for (int i = 0, j = 0; j != Math_.Primes.Length; ++i)
            {
                var is_prime = (i == Math_.Primes[j]);
                Assert.True(Math_.IsPrime(i) == is_prime);
                if (is_prime)
                {
                    ++j;
                }
            }

            // Test some random larger primes
            var rng = new Random(123);

            for (int i = 0; i != 10000; ++i)
            {
                var value     = rng.Next();
                var prime_gtr = Math_.PrimeGtrEq(value);
                var prime_lss = Math_.PrimeLessEq(value);

                Assert.True(Math_.IsPrime(prime_gtr));
                Assert.True(Math_.IsPrime(prime_lss));
                if (prime_gtr != prime_lss)
                {
                    Assert.True(Math_.IsPrime(value) == false);
                }
                else
                {
                    Assert.True(value == prime_gtr);
                }
            }

            // Test some random larger primes
            Assert.True(Math_.IsPrime(95413) == true);
            Assert.True(Math_.IsPrime(168987071) == false);
            Assert.True(Math_.IsPrime(168987079) == true);
            Assert.True(Math_.IsPrime(161489287071) == false);
            Assert.True(Math_.IsPrime(161489287081) == true);
            Assert.True(Math_.IsPrime(341550071728319) == false);
            Assert.True(Math_.IsPrime(341550071728631) == true);
        }