Esempio n. 1
0
        public static int[] CircularPrimesBelow(int l)
        {
            HashSet <int> circularPrimes = new HashSet <int>();

            var primes = new SievePrimeNumberGenerator()
                         .GetPrimesBelowIntMaxValue()
                         .TakeWhile(p => p < l)
                         .ToList();

            foreach (var prime in primes)
            {
                if (!circularPrimes.Contains(prime))
                {
                    var rotations = prime.GetAllRotations();

                    if (AllRotationsArePrime(primes, rotations))
                    {
                        foreach (var rotation in rotations)
                        {
                            circularPrimes.Add(rotation);
                        }
                    }
                }
            }

            return(circularPrimes.ToArray());
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            // we know we only need 4 digit primes
            var primes = new SievePrimeNumberGenerator().GetPrimesBelowIntMaxValue().SkipWhile(p => p < 1000).TakeWhile(p => p <= 9999).ToList();

            // get all combinations where the primes form an arithmetic sequence
            var sequences = from p1 in primes
                            from p2 in primes
                            from p3 in primes
                            where p1 < p2 && p2 < p3
                            where p2 - p1 == p3 - p2
                            select new
            {
                p1 = p1.ToString(),
                p2 = p2.ToString(),
                p3 = p3.ToString()
            };

            // filter to only the sequences that are permutations of each other
            var perms = from tuple in sequences
                        where tuple.p1.GeneratePermutations().Contains(tuple.p2)
                        where tuple.p2.GeneratePermutations().Contains(tuple.p3)
                        select tuple.p1 + tuple.p2 + tuple.p3;

            // find the only sequence which isn't the known one
            var ans = perms.First(p => p != knownSequence);

            Console.WriteLine(ans);

            Console.WriteLine("Finished");
            Console.ReadKey();
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            var answer = new SievePrimeNumberGenerator().GetPrimesBelowLongMaxValue()
                         .Take(10001)
                         .ToList();

            Console.WriteLine(answer.Last());
            Console.ReadKey();
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            var primes = new SievePrimeNumberGenerator().GetPrimesBelowLongMaxValue()
                         .TakeWhile(p => p < 2000000)
                         .ToList();

            var sum = primes.Sum();

            Console.WriteLine(sum);
            Console.ReadKey();
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            int upperBound = 1000000;

            int[] nums = new int[upperBound];

            var primes = new SievePrimeNumberGenerator().GetPrimesBelowIntMaxValue().TakeWhile(p => p <= upperBound);

            // for each multiple of a prime, increment it's count by 1 (we found another prime divisor of it)
            foreach (var prime in primes)
            {
                var num = prime + prime;
                while (num < nums.Length)
                {
                    nums[num]++;
                    num += prime;
                }
            }

            // find the first set of 4 4s
            int start = 0, count = 0;

            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] == 4)
                {
                    count++;
                    if (count == 1)
                    {
                        start = i;
                    }
                    else if (count == 4)
                    {
                        Console.WriteLine(start);
                        break;
                    }
                }
                else
                {
                    start = 0;
                    count = 0;
                }
            }


            Console.ReadKey();
        }
Esempio n. 6
0
        public static void Main(String[] args)
        {
            var primes = new SievePrimeNumberGenerator().GetPrimesBelowIntMaxValue().TakeWhile(p => p <= 1000000).ToArray();

            for (int i = 2; i < primes.Length; i++)
            {
                for (int start = 0; start < primes.Length - i; start++)
                {
                    int[] digits = new int[i];
                    for (int offset = 0; offset < i; offset++)
                    {
                        digits[offset] = primes[start + offset];
                    }

                    int sumOfDigits = 0;

                    try
                    {
                        sumOfDigits = digits.Sum();
                    }
                    catch (ArithmeticException)
                    {
                        // number overflowed what int could handle, clearly not < 1 million
                        continue;
                    }

                    if (sumOfDigits >= 1000000)
                    {
                        break;
                    }
                    else if (primes.Contains(sumOfDigits))
                    {
                        Console.WriteLine("{0}: {1}", i, sumOfDigits);
                        break;
                    }
                }
            }

            Console.WriteLine("Finished");
            Console.ReadKey();
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            Stopwatch s      = Stopwatch.StartNew();
            var       primes = new SievePrimeNumberGenerator().GetPrimesBelowLongMaxValue().TakeWhile(p => p <= 98765432).Reverse();

            foreach (var prime in primes)
            {
                var primeStr = prime.ToString();

                if (primeStr.IsPandigital(primeStr.Length))
                {
                    Console.WriteLine(prime);
                    break;
                }
            }

            s.Stop();

            Console.WriteLine(s.Elapsed);
            Console.ReadKey();
        }
 public override void SetUp()
 {
     generator = new SievePrimeNumberGenerator();
 }