Exemple #1
0
        protected override string Solve()
        {
            int result = 0;

            for (int i = 1; i < 10; i++)
            {
                string numberString = string.Empty;

                for (int j = 1; j <= i; j++)
                {
                    numberString += j.ToString();
                }

                int number = int.Parse(numberString);

                int[] permutations = number.GetPermutations();

                foreach (int candidate in permutations)
                {
                    if (PrimeHelper.IsPrime(candidate) &&
                        candidate > result)
                    {
                        result = candidate;
                    }
                }
            }

            return(result.ToString("0"));
        }
        public void TestToPrime()
        {
            var input = new[]
            {
                18, 13, 68, 99, 93, 34, 11, 47, 64, 10,
                809, 3420, 9391, 8999, 5154, 8934, 5416, 5669, 2755, 3159,
                285080, 727881, 846232, 665370, 153194, 157833, 585896, 914054, 505284, 333258,
                49086597, 78119724, 76928802, 23260170, 1955743, 39360664, 10885879, 30169506, 65889970, 95425647,
                179424551, 179424571, 179424577, 179424601, 179424611, 179424617, 179424629, 179424667, 179424671, 179424673,
                1683899352, 883641873, 114883641, 1000000007, 1000000009, 1000000021, 1000000033, 1000000087, 1000000093
            };
            var output = new[]
            {
                19, 13, 71, 101, 97, 37, 11, 47, 67, 11,
                809, 3433, 9391, 8999, 5167, 8941, 5417, 5669, 2767, 3163,
                285091, 727891, 846233, 665381, 153247, 157837, 585899, 914117, 505301, 333269,
                49086613, 78119749, 76928807, 23260187, 1955747, 39360683, 10885891, 30169523, 65889997, 95425663,
                179424551, 179424571, 179424577, 179424601, 179424611, 179424617, 179424629, 179424667, 179424671, 179424673,
                1683899359, 883641893, 114883661, 1000000007, 1000000009, 1000000021, 1000000033, 1000000087, 1000000093
            };

            Assert.AreEqual(input.Length, output.Length, "setup");
            for (var index = 0; index < output.Length; index++)
            {
                Assert.AreEqual(output[index], PrimeHelper.ToPrime(input[index]));
                Assert.IsTrue(PrimeHelper.IsPrime(output[index]));
            }
        }
        public void TestIsPrime()
        {
            var input = new[]
            {
                18, 13, 68, 99, 93, 34, 11, 47, 64, 10,
                809, 3420, 9391, 8999, 5154, 8934, 5416, 5669, 2755, 3159,
                285080, 727881, 846232, 665370, 153194, 157833, 585896, 914054, 505284, 333258,
                49086597, 78119724, 76928802, 23260170, 1955743, 39360664, 10885879, 30169506, 65889970, 95425647,
                179424551, 179424571, 179424577, 179424601, 179424611, 179424617, 179424629, 179424667, 179424671, 179424673,
                1683899352, 883641873, 114883641, 1000000007, 1000000009, 1000000021, 1000000033, 1000000087, 1000000093, 2147483629
            };
            var output = new[]
            {
                0, 1, 0, 0, 0, 0, 1, 1, 0, 0,
                1, 0, 1, 1, 0, 0, 0, 1, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                0, 0, 0, 1, 1, 1, 1, 1, 1, 1
            };

            Assert.AreEqual(input.Length, output.Length, "setup");
            for (var index = 0; index < output.Length; index++)
            {
                Assert.AreEqual(output[index] == 1, PrimeHelper.IsPrime(input[index]));
            }
        }
Exemple #4
0
        protected override string Solve()
        {
            int result = 0;

            IEnumerable <long> primes = PrimeHelper.GeneratePrimes(999999);

            foreach (long prime in primes)
            {
                IEnumerable <long> rotations = NumberHelper.GetRotations(prime);

                int primeCount = 0;

                foreach (long rotation in rotations)
                {
                    if (PrimeHelper.IsPrime(rotation))
                    {
                        primeCount++;
                    }
                }

                if (primeCount == rotations.Count())
                {
                    result++;
                }
            }

            return(result.ToString("0"));
        }
Exemple #5
0
        //The prime factors of 13195 are 5, 7, 13 and 29.

        //What is the largest prime factor of the number 600851475143 ?
        public static long Execute()
        {
            const long value        = 600851475143;
            long       largestPrime = 0;
            var        max          = Math.Ceiling(Math.Sqrt(value));

            for (long i = 2; i < max; i++)
            {
                if (value % i != 0)
                {
                    continue;
                }

                if (PrimeHelper.IsPrime(i))
                {
                    largestPrime = i;
                }

                var divider = value / i;

                if (!PrimeHelper.IsPrime(divider))
                {
                    continue;
                }

                largestPrime = divider;
                i            = divider;
            }

            return(largestPrime);
        }
Exemple #6
0
 public void TestBasicNonPrimes1To10()
 {
     int[] notPrimes = { 1, 4, 6, 8, 9, 10 };
     foreach (int notPrime in notPrimes)
     {
         Assert.IsFalse(PrimeHelper.IsPrime(notPrime), $"{notPrime} was found to be prime");
     }
 }
Exemple #7
0
 public void TestBasicPrimes1To10()
 {
     int[] primes = { 2, 3, 5, 7 };
     foreach (int prime in primes)
     {
         Assert.IsTrue(PrimeHelper.IsPrime(prime), $"{prime} was not found to be prime");
     }
 }
 public void IsPrimesTest()
 {
     Assert.IsFalse(PrimeHelper.IsPrime(-3));
     Assert.IsFalse(PrimeHelper.IsPrime(-1));
     Assert.IsFalse(PrimeHelper.IsPrime(0));
     Assert.IsFalse(PrimeHelper.IsPrime(1));
     Assert.IsFalse(PrimeHelper.IsPrime(4));
     Assert.IsFalse(PrimeHelper.IsPrime(10));
     Assert.IsTrue(PrimeHelper.IsPrime(2));
     Assert.IsTrue(PrimeHelper.IsPrime(3));
     Assert.IsTrue(PrimeHelper.IsPrime(5));
     Assert.IsTrue(PrimeHelper.IsPrime(7));
     Assert.IsTrue(PrimeHelper.IsPrime(397));
 }
        public void NextPrimesTest()
        {
            for (int n = 1; n < int.MaxValue / 2; n <<= 1)
            {
                int prime = PrimeHelper.NextPrime(n);
                Assert.GreaterOrEqual(prime, n);
                Assert.IsTrue(PrimeHelper.IsPrime(prime));

                if (n > 1)
                {
                    // Prime number should be close to actual number.
                    Assert.LessOrEqual(prime, (int)(n * 1.5));
                }
            }
        }
Exemple #10
0
        static void Main(string[] args)
        {
            Console.WriteLine("--- Primes between 0 and 100 ---");

            for (int i = 0; i < 100; i++)
            {
                bool prime = PrimeHelper.IsPrime(i);
                if (prime)
                {
                    Console.Write("Prime: ");
                    Console.WriteLine(i);
                }
            }

            Console.ReadLine();
        }
Exemple #11
0
        /// <summary>
        /// 500000 gives 7368787 and takes 8.3 seconds
        /// </summary>
        /// <returns></returns>
        public static int FirstRun()
        {
            var maxPrimeCount = 500000;
            var primeCount    = 0;
            var i             = 0;

            while (primeCount <= maxPrimeCount)
            {
                i++;
                if (PrimeHelper.IsPrime(i))
                {
                    primeCount++;
                }
            }

            return(i);
        }
        public static int GetPrime(int min)
        {
            if (min < 0)
            {
                throw new ArgumentException("min < 0");
            }

            for (int i = 0; i < PrimeHelper.Primes.Length; i++)
            {
                int prime = PrimeHelper.Primes[i];
                if (prime >= min)
                {
                    return(prime);
                }
            }
            for (int i = min | 1; i < 2147483647; i += 2)
            {
                if (PrimeHelper.IsPrime(i) && (i - 1) % 101 != 0)
                {
                    return(i);
                }
            }
            return(min);
        }
Exemple #13
0
        protected override string Solve()
        {
            int highestPrimeCount = 0;
            int resultA           = 0;
            int resultB           = 0;

            for (int a = -1000; a < 1000; a++)
            {
                for (int b = -1000; b < 1000; b++)
                {
                    int primeCount = 0;

                    for (int n = 0; true; n++)
                    {
                        int temp = n * n + a * n + b;

                        if (PrimeHelper.IsPrime(temp))
                        {
                            primeCount++;
                            if (primeCount > highestPrimeCount)
                            {
                                highestPrimeCount = primeCount;
                                resultA           = a;
                                resultB           = b;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            return((resultA * resultB).ToString("0"));
        }
Exemple #14
0
    public void IsPrimeSquare()
    {
        var actual = PrimeHelper.IsPrime(25);

        Assert.False(actual);
    }
Exemple #15
0
        protected override string Solve()
        {
            int hitCount = 0;
            int result   = 0;

            IList <long> primes = PrimeHelper.GeneratePrimes(100000000).ToList();

            primes.Remove(2);
            primes.Remove(3);
            primes.Remove(5);
            primes.Remove(7);

            for (int i = 0; i < primes.Count; i++)
            {
                bool primeCheck = true;

                int n = i;

                //check primality right to left
                do
                {
                    if (PrimeHelper.IsPrime(n) == false)
                    {
                        primeCheck = false;
                        break;
                    }
                    n /= 10;
                } while (n > 0);

                //check primality left to right
                if (primeCheck) //don't bother if already failed right to left
                {
                    n = i;
                    int modulo = (int)Math.Pow(10, (NumberHelper.GetDigits(n).Count() - 1));
                    do
                    {
                        n %= modulo;

                        modulo = (int)Math.Pow(10, (NumberHelper.GetDigits(n).Count() - 1));

                        if (PrimeHelper.IsPrime(n) == false)
                        {
                            primeCheck = false;
                            break;
                        }
                    } while (modulo != 1);
                }


                if (primeCheck)
                {
                    result += i;
                    hitCount++;
                }
                if (hitCount == 11)
                {
                    return(result.ToString("0"));
                }
            }

            return("none found");
        }
Exemple #16
0
        // 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
        // What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

        // 25 gives 26771144400 and takes 0.01 seconds
        // 100 takes 0.01 seconds
        public static BigInteger Execute()
        {
            const int max          = 20;
            var       primeFactors = new Dictionary <int, int>();

            for (var i = 1; i <= max; i++)
            {
                var smallestPrime = 1;
                if (i % 2 == 0)
                {
                    smallestPrime = 2;
                }
                else
                {
                    for (var j = 3; j <= i; j = j + 2)
                    {
                        if (i % j != 0)
                        {
                            continue;
                        }
                        if (!PrimeHelper.IsPrime(j))
                        {
                            continue;
                        }
                        smallestPrime = j;
                        break;
                    }
                }

                var power  = 0;
                var factor = i;
                while (factor % smallestPrime == 0)
                {
                    if (factor == 1)
                    {
                        break;
                    }
                    factor = factor / smallestPrime;
                    power++;
                }

                if (primeFactors.ContainsKey(smallestPrime))
                {
                    if (primeFactors[smallestPrime] < power)
                    {
                        primeFactors[smallestPrime] = power;
                    }
                }
                else
                {
                    primeFactors[smallestPrime] = power;
                }
            }

            BigInteger value = 1;

            foreach (var factor in primeFactors)
            {
                value = value * (BigInteger)Math.Pow(factor.Key, factor.Value);
            }
            return(value);
        }
Exemple #17
0
    public void IsPrime2To4()
    {
        var actual = PrimeHelper.IsPrime(3);

        Assert.True(actual);
    }
Exemple #18
0
 public void IsPrime_ReturnsCorrectResult(int number, bool expectedResult)
 {
     PrimeHelper.IsPrime(number).Should().Be(expectedResult);
 }
Exemple #19
0
    public void IsPrimeEven()
    {
        var actual = PrimeHelper.IsPrime(10);

        Assert.False(actual);
    }
Exemple #20
0
    public void IsPrimeOdds4To9()
    {
        var actual = PrimeHelper.IsPrime(7);

        Assert.True(actual);
    }
        public async Task <int> AddThreadAsync()
        {
            CancellationTokenSource source = new CancellationTokenSource();
            CancellationToken       token  = source.Token;

            Task producer = Task.Run(async() => {
                while (!token.IsCancellationRequested)
                {
                    if (_documents.TryDequeue(out var file))
                    {
                        var numbers = new List <int>();

                        string line;
                        while ((line = file.Stream.ReadLine()) != null)
                        {
                            numbers.Add(Int32.Parse(line));
                        }

                        _channel.Writer.TryWrite(new ChannelData()
                        {
                            FileName = file.FileName, FileData = numbers
                        });
                    }
                    else
                    {
                        _channel.Writer.TryComplete();
                    }
                }
            }, token);

            Task consumer = Task.Run(async() => {
                while (!token.IsCancellationRequested && await _channel.Reader.WaitToReadAsync())
                {
                    if (_channel.Reader.TryRead(out var file))
                    {
                        var count = file.FileData.Count;
                        foreach (var number in file.FileData)
                        {
                            var isPrime = PrimeHelper.IsPrime(number);
                            if (isPrime)
                            {
                                _logger.LogInformation(file.FileName + " : " + number.ToString() + " : " + count);

                                if (_minPrime > number)
                                {
                                    ChangeMin(number);
                                }
                                if (_maxPrime < number)
                                {
                                    ChangeMax(number);
                                }
                            }

                            //Otherwise all the files are checked too fast
                            await Task.Delay(DelayTime);
                            count--;

                            ChangeFileCompleted(file.FileName, file.FileData.Count, count);
                        }
                    }
                    IncrementTotalFilesDone();
                }
            }, token);

            var consumerProducer = new ConsumerProducer {
                Producer = producer, Consumer = consumer
            };

            _threads.Add(source, consumerProducer);

            return(await Task.FromResult(_threads.Count));
        }
Exemple #22
0
        public IActionResult Index(int matrix_size = 10, string matrix_base = "decimal")
        {
            if (matrix_size > 15)
            {
                matrix_size = 15;
            }
            else if (matrix_size < 3)
            {
                matrix_size = 3;
            }
            var matrix = new Matrix(matrix_size);

            var list = new List <CustomModel>
            {
                new CustomModel {
                    Value = "X", XAxis = null, YAxis = null
                }
            };

            for (int i = 1; i < matrix_size + 1; i++)
            {
                list.Add(new CustomModel {
                    Value = i.ToString(), XAxis = null, YAxis = null, IsPrime = PrimeHelper.IsPrime(i)
                });
            }
            matrix.Values.Add(list);
            for (int i = 1; i < matrix_size + 1; i++)
            {
                list = new List <CustomModel>
                {
                    new CustomModel {
                        Value = i.ToString(), XAxis = null, YAxis = null, IsPrime = PrimeHelper.IsPrime(i)
                    }
                };
                for (int j = 1; j < matrix_size + 1; j++)
                {
                    switch (matrix_base)
                    {
                    case "hex":
                        list.Add(new CustomModel {
                            Value = (i * j).ToString("X"), XAxis = i, YAxis = j, IsPrime = PrimeHelper.IsPrime(i * j)
                        });
                        break;

                    case "binary":
                        list.Add(new CustomModel {
                            Value = Convert.ToString((i * j), 2), XAxis = i, YAxis = j, IsPrime = PrimeHelper.IsPrime(i * j)
                        });
                        break;

                    default:
                        list.Add(new CustomModel {
                            Value = (i * j).ToString(), XAxis = i, YAxis = j, IsPrime = PrimeHelper.IsPrime(i * j)
                        });
                        break;
                    }
                }

                matrix.Values.Add(list);
            }

            return(View(matrix));
        }
Exemple #23
0
    public void IsPrimeLessThan2()
    {
        var actual = PrimeHelper.IsPrime(1);

        Assert.False(actual);
    }
Exemple #24
0
    public void IsPrimeNotPrime()
    {
        var actual = PrimeHelper.IsPrime(8227);

        Assert.False(actual);
    }
Exemple #25
0
    public void IsPrimePrime()
    {
        var actual = PrimeHelper.IsPrime(8219);

        Assert.True(actual);
    }
Exemple #26
0
 private bool AreConcatenationsPrime(int a, int b)
 {
     return(PrimeHelper.IsPrime(ConcatenateNumbers(a, b)) && PrimeHelper.IsPrime(ConcatenateNumbers(b, a)));
 }
Exemple #27
0
    public void IsPrimeMultipleOfThree()
    {
        var actual = PrimeHelper.IsPrime(51);

        Assert.False(actual);
    }