Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var primesPow2 = Primes.GetPrimes((ulong)Math.Pow(uBound, 1 / 2D)).ToArray();
            var primesPow3 = Primes.GetPrimes((ulong)Math.Pow(uBound, 1 / 3D)).ToArray();
            var primesPow4 = Primes.GetPrimes((ulong)Math.Pow(uBound, 1 / 4D)).ToArray();
            var matching   = new HashSet <ulong>();

            for (int pow4idx = 0; pow4idx < primesPow4.Length; pow4idx++)
            {
                var pow4 = (ulong)Math.Pow(primesPow4[pow4idx], 4);
                for (int pow3idx = 0; pow3idx < primesPow3.Length; pow3idx++)
                {
                    var pow3 = (ulong)Math.Pow(primesPow3[pow3idx], 3);
                    if (pow4 + pow3 >= uBound)
                    {
                        break;
                    }
                    for (int pow2idx = 0; pow2idx < primesPow2.Length; pow2idx++)
                    {
                        var   pow2      = (ulong)Math.Pow(primesPow2[pow2idx], 2);
                        ulong candidate = pow2 + pow3 + pow4;
                        if (candidate > uBound)
                        {
                            break;
                        }
                        matching.Add(candidate);
                    }
                }
            }

            Console.WriteLine(matching.Count);
            Console.ReadLine();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var i = Primes.GetPrimes().Skip(10000).First();

            Debug.Assert(i == 104743);
            Console.WriteLine(i);

            Console.ReadLine();
        }
Exemplo n.º 3
0
        public void TestGetPrimes()
        {
            uint i = 0;

            foreach (uint prime in Primes.GetPrimes(3572))
            {
                Assert.AreEqual(_primes[i++], prime);
            }
        }
Exemplo n.º 4
0
        public void GetPrimes_Range()
        {
            Console.WriteLine(string.Join(" ", Primes.GetPrimes(998244000, 998245000)));
            Console.WriteLine(string.Join(" ", Primes.GetPrimes(1000000000, 1000001000)));
            Console.WriteLine(string.Join(" ", Primes.GetPrimes(1000000000000, 1000000001000)));
            Assert.AreEqual(1000, Primes.GetPrimes(22801763489, 22801787296).Length);

            var actual = TimeHelper.Measure(() => Primes.GetPrimes(1000000000000, 1000001000000));

            Console.WriteLine(actual.Length);
        }
Exemplo n.º 5
0
        static public void SumPrimesWithHelperClass(int number)
        {
            int[] primes      = Primes.GetPrimes(number);
            int   sumOfPrimes = 0;

            foreach (int prime in primes)
            {
                sumOfPrimes += prime;
            }

            Console.WriteLine("There are {0} Prime numbers below {1}.", primes.Length, number);
            Console.WriteLine("The sum of the Prime numbers is {0}", sumOfPrimes);
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            var   primes = Primes.GetPrimes(2000000);
            ulong sum    = 0;

            foreach (var p in primes)
            {
                sum += p;
            }

            Console.WriteLine(sum);
            Console.ReadLine();
        }
Exemplo n.º 7
0
        static void Main()
        {
            var primes         = Primes.GetPrimes(999999);
            var circularPrimes = primes.Where(u => IsCircular(u));

            foreach (var item in circularPrimes)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine(circularPrimes.Count());

            Console.ReadLine();
        }
Exemplo n.º 8
0
        public void GetPrimes_Range_All()
        {
            for (int i = 1; i <= 100; i++)
            {
                Test(i);
            }

            Test(100000);
            Test(1000000);

            void Test(int M)
            {
                CollectionAssert.AreEqual(Primes.GetPrimes(M), Primes.GetPrimes(1, M));
            }
        }
Exemplo n.º 9
0
        public string Solve(SingleLimitProblemArgs arguments)
        {
            long result = 1;

            foreach (var factor in Primes.GetPrimes(arguments.Limit))
            {
                long n = 1;
                while (n * factor <= arguments.Limit)
                {
                    n *= factor;
                }

                result *= n;
            }

            return(result.ToString());
        }
Exemplo n.º 10
0
        static void Main()
        {
            var matching = from n in Primes.GetPrimes().SkipWhile(x => x < 23)
                           where n % 10 != 1
                           where RightToLeftAreAllPrimes(n)
                           where LeftToRightAreAllPrimes(n)
                           select n;

            foreach (var item in matching.Take(11))
            {
                Console.WriteLine(item);
            }

            var result = matching.Take(11).Sum();

            Console.WriteLine(result);
            Console.ReadLine();
        }
Exemplo n.º 11
0
        private static void Main(string[] args)
        {
            /*
             * The prime factors of 13195 are 5, 7, 13 and 29.
             * What is the largest prime factor of the number 600851475143 ?
             */

            Stopwatch timer = new Stopwatch();

            Console.WriteLine("Find the Largest Prime factor of a number!");
            Console.WriteLine("------------------------------------------");
            Console.WriteLine();
            Console.Write("Enter the number you want the Largest Prime factor of: ");
            long original = Convert.ToInt64(Console.ReadLine());
            int  largest  = 0;

            timer.Start();
            // Store all primes up to half the supplied number
            int[] primes = Primes.GetPrimes((int)Math.Sqrt(original));

            // List to store all the prime factors of the supplied number
            List <int> primeFactors = new List <int>();

            foreach (int prime in primes)
            {
                if (original % prime == 0)
                {
                    primeFactors.Add(prime);
                }
            }

            foreach (int primeFactor in primeFactors)
            {
                if (primeFactor > largest)
                {
                    largest = primeFactor;
                }
            }
            timer.Stop();
            Console.WriteLine();
            Console.WriteLine("The largest factor of {0} is {1}.", original, largest);
            Console.WriteLine($"Total run time: {timer.Elapsed}");
            Console.ReadLine();
        }        //end Main
Exemplo n.º 12
0
        public void GetPrimesTest()
        {
            var primes     = Primes.GetPrimes();
            var enumerator = primes.GetEnumerator();

            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual(enumerator.Current, 2UL);
            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual(enumerator.Current, 3UL);

            do
            {
                Assert.IsTrue(enumerator.MoveNext());
            } while (enumerator.Current < 997); // Last prime below 1000

            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual(enumerator.Current, 1009UL);
            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual(enumerator.Current, 1013UL);
        }
Exemplo n.º 13
0
        public void GetPrimes()
        {
            Console.WriteLine(string.Join(" ", Primes.GetPrimes(100)));

            Test(1, 0);
            Test(2, 1);
            Test(10, 4);
            Test(100, 25);
            Test(1000, 168);
            Test(10000, 1229);
            Test(100000, 9592);
            Test(1000000, 78498);
            Test(10000000, 664579);

            void Test(long n, int expected)
            {
                var actual = TimeHelper.Measure(() => Primes.GetPrimes(n));

                Assert.AreEqual(expected, actual.Length);
            }
        }
Exemplo n.º 14
0
        public void Sample()
        {
            // 素因数分解
            Console.WriteLine(string.Join(" * ", Primes.Factorize(2020)));
            // 2 * 2 * 5 * 101

            // 約数の列挙
            Console.WriteLine(string.Join(" ", Primes.Divisors(2020)));
            // 1 2 4 5 10 20 101 202 404 505 1010 2020

            // 素数判定
            Console.WriteLine(Primes.IsPrime(1000000007));
            // True

            // n 以下の素数の列挙
            Console.WriteLine(string.Join(" ", Primes.GetPrimes(100)));
            // 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

            // m 以上 M 以下の素数の列挙
            Console.WriteLine(string.Join(" ", Primes.GetPrimes(1000000000, 1000000100)));
            // 1000000007 1000000009 1000000021 1000000033 1000000087 1000000093 1000000097
        }
Exemplo n.º 15
0
        public void GetPrimes_ValidMaxValue_ReturnsCorrectSizedArray()
        {
            int[] result = Primes.GetPrimes(5);

            Assert.That(result.Length, Is.EqualTo(4));
        }
Exemplo n.º 16
0
        public void GetPrimes_ValidMaxValue_ReturnsCorrectArrayOfValues()
        {
            int[] result = Primes.GetPrimes(5);

            Assert.That(result, Is.EquivalentTo(new int[] { 1, 2, 3, 5 }));
        }
Exemplo n.º 17
0
        public void GetPrimes_ShouldReturnCorrectSequence(int upperLimit, long[] expectedValues)
        {
            var values = Primes.GetPrimes(upperLimit);

            Assert.IsTrue(values.SequenceEqual(expectedValues));
        }
Exemplo n.º 18
0
 public string GetSolution()
 {
     return(Primes.GetPrimes(10001)
            .Last()
            .ToString());
 }
Exemplo n.º 19
0
 public void GetPrimes_InvalidValues_ThrowsArgumentOutOfRangeException(int maxValue)
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => Primes.GetPrimes(maxValue));
 }
Exemplo n.º 20
0
        public static void ShouldReturnArgumentException(this string input)
        {
            var primes = new Primes();

            Assert.Throws(typeof(ArgumentException), () => primes.GetPrimes(input));
        }
Exemplo n.º 21
0
        public long Solve()
        {
            List <uint> primes   = new List <uint>(Primes.GetPrimes(1000000));
            List <uint> circular = new List <uint>();

            foreach (uint prime in primes)
            {
                if (prime < 10)
                {
                    circular.Add(prime);
                    continue;
                }

                if (circular.Contains(prime))
                {
                    continue;
                }

                var digits = Utility.GetDigits(prime);

                // A circular prime with at least two digits can only consist of combinations of the digits
                // 1, 3, 7 or 9, because having 0, 2, 4, 6 or 8 as the last digit makes the number divisible
                // by 2, and having 0 or 5 as the last digit makes it divisible by 5.
                bool skip = false;
                foreach (uint digit in digits)
                {
                    if (digit != 1 && digit != 3 && digit != 7 && digit != 9)
                    {
                        skip = true;
                        break;
                    }
                }
                if (skip)
                {
                    continue;
                }

                List <uint> candidates = new List <uint>();
                candidates.Add(prime);
                bool allFound = true;
                for (int i = 1; i < digits.Count; i++)
                {
                    uint candidate = 0;
                    for (int j = digits.Count - 1; j >= 0; j--)
                    {
                        candidate += digits[(i + j) % digits.Count];
                        if (j > 0)
                        {
                            candidate *= 10;
                        }
                    }
                    if (!primes.Contains(candidate))
                    {
                        allFound = false;
                        break;
                    }
                    if (!candidates.Contains(candidate))
                    {
                        candidates.Add(candidate);
                    }
                }
                if (allFound)
                {
                    circular.AddRange(candidates);
                }
            }
            return(circular.Count);
        }
Exemplo n.º 22
0
        public static void ShouldReturnPrimeNumbers(this string input, int[] expected)
        {
            var primes = new Primes();

            Assert.Equal(expected, primes.GetPrimes(input));
        }
Exemplo n.º 23
0
        public long Solve()
        {
            int  count = 0;
            long sum   = 0;

            List <uint> primes = new List <uint>(Primes.GetPrimes(1000000));

            foreach (uint prime in primes)
            {
                if (prime < 10)
                {
                    continue;
                }

                bool skip = false;

                var digits = Utility.GetDigits(prime);

                // Can only consist of combinations of the digits
                // 1, 3, 7 or 9, because having 0, 2, 4, 6 or 8 as the last digit makes the number divisible
                // by 2, and having 0 or 5 as the last digit makes it divisible by 5.
                foreach (uint digit in digits)
                {
                    if (digit != 1 && digit != 3 && digit != 7 && digit != 9)
                    {
                        skip = true;
                        break;
                    }
                }
                if (skip)
                {
                    continue;
                }

                // Truncate left
                uint l = Utility.TruncateLeft(prime);
                while (!skip && l > 0)
                {
                    if (!primes.Contains(l))
                    {
                        skip = true;
                    }
                    l = Utility.TruncateLeft(l);
                }

                // Truncate right
                uint r = Utility.TruncateRight(prime);
                while (!skip && r > 0)
                {
                    if (!primes.Contains(r))
                    {
                        skip = true;
                    }
                    r = Utility.TruncateRight(r);
                }

                if (!skip)
                {
                    count++;
                    sum += prime;
                }

                if (count == 11)
                {
                    break;
                }
            }
            return(sum);
        }