Ejemplo n.º 1
0
        public string Execute()
        {
            primes = new HashSet <long>(Primes.GeneratePrimes(1, 20000));

            var highestYield = 0;
            var bestA        = 0;
            var bestB        = 0;


            var range = 1000;

            for (var a = -range; a < range; a++)
            {
                for (var b = -range; b <= range; b++)
                {
                    var total = CalculatePrimes(a, b);
                    if (total > highestYield)
                    {
                        bestA        = a;
                        bestB        = b;
                        highestYield = total;
                    }
                }
            }
            return((bestA * bestB).ToString());
        }
Ejemplo n.º 2
0
        public void primes_first_10_numbers()
        {
            var actual   = Primes.GeneratePrimes(10);
            var expected = Primes.WritePrimes(10);

            Assert.IsTrue(ElementsOnListsAreTheSame(actual, expected));
        }
Ejemplo n.º 3
0
        public string Execute()
        {
            var maximum = 28123;
            var primes  = new HashSet <long>(Primes.GeneratePrimes(1, maximum));

            abundantValues = new HashSet <long>();

            for (var i = 1; i <= maximum; i++)
            {
                if (!primes.Contains(i))
                {
                    var div = Utility.GetDistinctDivisors(i);
                    var sum = div.Sum();
                    if (sum > i)
                    {
                        var abundant = i;
                        abundantValues.Add(i);
                    }
                }
            }

            var result = 0;

            for (var i = 1; i <= maximum; i++)
            {
                if (!SumAbundantLookup(i))
                {
                    result += i;
                }
            }

            return(result.ToString());
        }
Ejemplo n.º 4
0
        public string Execute()
        {
            var pl        = Primes.GeneratePrimes(1000, 10000);
            var lut       = new HashSet <string>();
            var itemsList = new Dictionary <string, List <int> >();

            foreach (var p in pl)
            {
                var cl = new List <char>(p.ToString().ToCharArray());
                cl.Sort();
                var ss = string.Join("", cl);
                if (!lut.Contains(ss))
                {
                    lut.Add(ss);
                    var items = new List <int> {
                        (int)p
                    };
                    itemsList.Add(ss, items);
                }
                else
                {
                    var items = itemsList[ss];
                    items.Add((int)p);
                }
            }

            foreach (var i in itemsList)
            {
                var valueList = i.Value;
                if (valueList.Count >= 3)
                {
                    for (var a = 0; a < valueList.Count; a++)
                    {
                        var valuea = valueList[a];

                        for (var b = a + 1; b < valueList.Count; b++)
                        {
                            var valueb = valueList[b];
                            var diff   = valueb - valuea;

                            for (var c = valueList.Count - 1; c > b; c--)
                            {
                                var valuec = valueList[c];
                                var diff2  = valuec - valueb;
                                if (diff2 == diff && valuea != 1487)
                                {
                                    return($"{valuea}{valueb}{valuec}");
                                }
                            }
                        }
                    }
                }
            }
            return(string.Empty);
        }
Ejemplo n.º 5
0
        public string Execute()
        {
            //sum of a prime and 2x a square
            //for every odd composite

            var max    = (int)Math.Pow(10, 4);
            var primes = Primes.GeneratePrimes(1, max);

            var primeLut = new HashSet <long>(primes);


            var squares = new HashSet <long>();

            for (var s = 1; s <= max; s++)
            {
                squares.Add(2 * (int)Math.Pow(s, 2));
            }

            for (var i = 3; i <= max; i += 2)
            {
                if (primeLut.Contains(i))
                {
                    continue;
                }
                var found = false;
                foreach (var p in primes)
                {
                    if (p >= i)
                    {
                        break;
                    }
                    var diff = i - p;

                    if (diff > 0 && squares.Contains(diff))
                    {
                        var sq = Math.Sqrt(diff / 2);
                        // Debug.Print($"{i} = {p} + 2*({sq}^2)");

                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    return(i.ToString());
                }
            }
            return(string.Empty);
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            double numberToOperations = -5.8;
            int    moduloDivide       = 105;
            int    moduloDivideBy     = 9;
            int    howLongPermutation = 30;
            int    howManyPrimes      = 10;
            var    primes             = Primes.GeneratePrimes(howManyPrimes);

            Console.WriteLine("Start");
            Console.WriteLine("----------");
            Console.WriteLine("Round roof: " + numberToOperations);
            Console.WriteLine("Result: ");
            Console.WriteLine(Operations.RoundRoof(numberToOperations));
            Console.WriteLine("----------");
            Console.WriteLine("Round floor: " + numberToOperations);
            Console.WriteLine("Result: ");
            Console.WriteLine(Operations.RoundFloor(numberToOperations));
            Console.WriteLine("----------");
            Console.WriteLine("Fraction part: " + numberToOperations);
            Console.WriteLine("Result: ");
            Console.WriteLine(Operations.Fraction(numberToOperations));
            Console.WriteLine("----------");
            Console.WriteLine("Modulo: {0} % {1}", moduloDivide, moduloDivideBy);
            Console.WriteLine("Result: ");
            Console.WriteLine(Modulo.Division(moduloDivide, moduloDivideBy));
            Console.WriteLine("----------");
            Console.WriteLine("First {0} primes:", howManyPrimes);
            Console.WriteLine("Result: ");
            Primes.WriteList(primes);
            Console.WriteLine("----------");
            Console.WriteLine("Permutations of {0} numbers:", howLongPermutation);
            Permutation.WritePermutations(howLongPermutation);
            Console.WriteLine("----------");

            Console.WriteLine("\n\n\n Done");

            Console.ReadKey();
        }
Ejemplo n.º 7
0
        public string Execute()
        {
            primes    = Primes.GeneratePrimes(1, 150000);
            primesLut = new HashSet <long>(primes);

            var sequenceCount = 4;

            for (var primeIndex = 0; primeIndex < primes.Count - 1; primeIndex++)
            {
                var nextPrime = primes[primeIndex + 1];

                var index = primes[primeIndex];
                while (index < nextPrime)
                {
                    var foundCount = 0;
                    for (var offset = 1; offset <= sequenceCount; offset++)
                    {
                        var value = index + offset;
                        if (!primesLut.Contains(value))
                        {
                            var factors = DistinctPrimeFactors(value);
                            if (factors.Count == sequenceCount)
                            {
                                foundCount++;
                                continue;
                            }
                            break;
                        }
                    }
                    index++;
                    if (foundCount == sequenceCount)
                    {
                        return(index.ToString());
                    }
                }
            }
            return(string.Empty);
        }