Ejemplo n.º 1
0
        public void PrimeSieveOfEratosthenes_BigInteger_ReturnsPrimes()
        {
            var value  = 449243;
            var result = PrimeUtility.PrimeSieveOfEratosthenes(449244);

            Assert.AreEqual(value, result.Last());
        }
Ejemplo n.º 2
0
        public void CalculateLargestPrimeFactor()
        {
            List <double> results   = new List <double>();
            double        remainder = GoalNumber;

            // Process
            NumericSources.YieldNextPrime().TakeWhile(x => ProcessNumber(x) && x < remainder / 2).ToList();

            // Potentially recursively check a single number if it's a factor
            bool ProcessNumber(double i)
            {
                while (IsFactor(i))
                {
                    AdjustRemainder(i);
                    results.Add(i);

                    // Once the remainder is a prime, stop.
                    if (PrimeUtility.IsNumberPrime(remainder))
                    {
                        results.Add(remainder);
                        MaxPrimeOrFactor = remainder;
                        return(false);
                    }
                }

                return(true);
            }

            bool IsFactor(double i) => remainder % i == 0;
            void AdjustRemainder(double i) => remainder = remainder / i;
        }
Ejemplo n.º 3
0
        public void LargestPrimeTestCaseorNEW_Integers_ReturnsResult(long n, long value)
        {
            var p      = PrimeUtility.PrimeSieveOfEratosthenes((int)Math.Sqrt(n));
            var result = PrimeUtility.LargestPrimeFactorUsingSieve(n, p);

            Assert.AreEqual(value, result);
        }
Ejemplo n.º 4
0
        public static long GeneralizedSolution(int n)
        {
            var primes = PrimeUtility.PrimeSieveOfEratosthenes(n);

            long sum = primes.Select(x => (long)x).Sum();

            return(sum);
        }
Ejemplo n.º 5
0
        private static List <long> ReturnLargestPrimeFactors(long n)
        {
            var summand = new List <long>();

            for (long i = 2; i <= n; i++)
            {
                summand.Add(PrimeUtility.LargestPrimeFactor(i));
            }

            return(summand);
        }
Ejemplo n.º 6
0
        public static long Solution()
        {
            // see links:
            // https://en.wikipedia.org/wiki/Prime_number_theorem#Approximations_for_the_nth_prime_number
            // https://math.stackexchange.com/questions/1270814/bounds-for-n-th-prime

            const long n = 10001;
            const int  N = 114319; // n*(log(n) + log(log(n)))

            var primes = PrimeUtility.PrimeSieveOfEratosthenes(N);

            return(primes[(int)(n - 1)]);
        }
Ejemplo n.º 7
0
        public void PrimeSieveOfEratosthenes_Integer_ReturnsPrimes()
        {
            var primes = new List <int>()
            {
                2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59,
                61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127,
                131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191,
                193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257,
                263, 269, 271
            };
            var result = PrimeUtility.PrimeSieveOfEratosthenes(271);

            Assert.AreEqual(primes, result);
        }
Ejemplo n.º 8
0
        public static int Solution(int max)
        {
            var primes = PrimeUtility.PrimeSieveOfEratosthenes(max).ToArray();

            for (int i = 9; i <= max; i += 2)
            {
                if (!i.IsPrime())
                {
                    if (!IsSumOfPrimeAndTwiceSquare(i))
                    {
                        return(i);
                    }
                }
            }

            return(-1);
        }
Ejemplo n.º 9
0
        public static bool IsSumOfPrimeAndTwiceSquare(int n)
        {
            var primes = PrimeUtility.PrimeSieveOfEratosthenes(n);

            foreach (var prime in primes)
            {
                for (int i = 1; i <= n; i++)
                {
                    var condition = prime + 2 * i * i == n;
                    if (condition)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 10
0
        public static int GeneralizedSolution(int size, int max)
        {
            var primes = PrimeUtility.PrimeSieveOfEratosthenes(max);

            foreach (var p in primes)
            {
                var digitReplacementCombinations = DigitReplacementCombinations(p);
                foreach (var combination in digitReplacementCombinations)
                {
                    var numberFamily = DigitReplacementFamily(p, combination.ToArray());
                    var primeFamily  = numberFamily.Where(x => x.IsPrime());

                    if (primeFamily.Count() == size)
                    {
                        return(primeFamily.First());
                    }
                }
            }

            return(0);
        }
Ejemplo n.º 11
0
        public static long SolutionForArbitraryInputUsingSieve(long n)
        {
            var m = (int)Math.Sqrt(n);
            var p = PrimeUtility.PrimeSieveOfEratosthenes(m);

            /*var summand = new List<long>();
             *
             * for (long i = 2; i <= n; i++)
             * {
             *  summand.Add(PrimeUtility.LargestPrimeFactorUsingSieve(i, p));
             * }
             *
             * return summand.Sum();*/
            long sum = 0;

            for (long i = 2; i <= n; i++)
            {
                sum += PrimeUtility.LargestPrimeFactorUsingSieve(i, p);
            }

            return(sum);
        }
Ejemplo n.º 12
0
 private static bool IsTripletCondition(int m, int n)
 {
     return((m - n) % 2 == 1 &&                             // difference is odd
            !(m % 2 == 1 && n % 2 == 1) &&                  // both not odd
            PrimeUtility.GreatestCommonDivisor(m, n) == 1); // co-prime
 }
Ejemplo n.º 13
0
 public static long Solution()
 {
     return(PrimeUtility.LargestPrimeFactor(600851475143));
 }
Ejemplo n.º 14
0
        public void LargestPrimeFactor_Integers_ReturnsResult(long n, long value)
        {
            var result = PrimeUtility.LargestPrimeFactor(n);

            Assert.AreEqual(value, result);
        }
Ejemplo n.º 15
0
        public void GreatestCommonDivisor_Integers_ReturnsResult(BigInteger a, BigInteger b, BigInteger value)
        {
            var result = PrimeUtility.GreatestCommonDivisor(a, b);

            Assert.AreEqual(value, result);
        }
Ejemplo n.º 16
0
        public void PrimeSieveOfEratosthenes_Integers_ReturnsPrimes(int x, int y)
        {
            var result = PrimeUtility.PrimeSieveOfEratosthenes(x);

            Assert.AreEqual(y, result.Last());
        }