Esempio n. 1
0
        public void IsNotPrime()
        {
            /* Arrange */
            /* Act */
            var result = _primeService.IsPrime(1);

            /* Assert */
            Assert.False(result, "1 should not be prime");
        }
Esempio n. 2
0
        public void ReturnFalseGivenValueOf1()
        {
            //Arrange
            var testData = 1;
            //Act
            var result = primeService.IsPrime(testData);

            //Assert
            Assert.False(result, "1 should not be prime");
        }
Esempio n. 3
0
        public void IsPrime_ShouldReturnFalse_IfNumberLessThanOrEqToZero()
        {
            _Sut = new PrimeService(PrimeAlgorithmType_Values.BruteForce);

            bool actual = _Sut.IsPrime(0);

            Assert.IsFalse(actual, "0(zero) is not a prime number");

            actual = _Sut.IsPrime(-1);
            Assert.IsFalse(actual, "-1 is not a prime number");
        }
Esempio n. 4
0
        public override string GetAnswer()
        {
            for (int i = 3; ; i += 2)
            {
                if (_primeService.IsPrime(i))
                {
                    continue;
                }

                if (FitsRule(i))
                {
                    return(i.ToString());
                }
            }
        }
Esempio n. 5
0
        public override string GetAnswer()
        {
            int    sideLength    = 1;
            double primeCount    = 0;
            int    diagonalCount = 1;
            int    currentNumber = 1;

            do
            {
                sideLength += 2;

                for (int i = 0; i < 4; i++)
                {
                    currentNumber += sideLength - 1;

                    if (_primeService.IsPrime(currentNumber))
                    {
                        primeCount++;
                    }

                    diagonalCount++;
                }
            } while (primeCount / diagonalCount > 0.1);

            return(sideLength.ToString());
        }
Esempio n. 6
0
        public override string GetAnswer()
        {
            // The sum of the primes up to 3943 = 1,001,604. No need to go higher.
            var primes = PrimeUtilities.GeneratePrimesUpToN(3943);

            // 547 primes under 3943.
            for (int length = 547; length > 0; --length)
            {
                for (int offset = 0; offset <= primes.Count - length; ++offset)
                {
                    int sum = 0;
                    for (int i = 0; i < length; ++i)
                    {
                        sum += primes[offset + i];
                    }

                    if (_primeService.IsPrime(sum))
                    {
                        return(sum.ToString());
                    }
                }
            }

            return(null);
        }
Esempio n. 7
0
        public override string GetAnswer()
        {
            const int limit      = 1000000;
            int       primeCount = 1;

            for (int i = 3; i < limit; i += 2)
            {
                if (!_primeService.IsPrime(i))
                {
                    continue;
                }

                var  perm            = new List <char>(i.ToString().ToCharArray());
                bool isCircularPrime = true;

                for (int j = 0; isCircularPrime && j < perm.Count; ++j)
                {
                    if (perm[perm.Count - 1] % 2 == 0)
                    {
                        isCircularPrime = false;
                        break;
                    }

                    if (!_primeService.IsPrime(int.Parse(new string(perm.ToArray()))))
                    {
                        isCircularPrime = false;
                        break;
                    }

                    // Rotate number.
                    for (int k = perm.Count - 2; k >= 0; --k)
                    {
                        char temp = perm[k + 1];
                        perm[k + 1] = perm[k];
                        perm[k]     = temp;
                    }
                }

                if (isCircularPrime)
                {
                    ++primeCount;
                }
            }

            return(primeCount.ToString());
        }
Esempio n. 8
0
        public void IsPrime_ShouldReturnTrue_IfNumberIsPrime(int number)
        {
            _Sut = new PrimeService(PrimeAlgorithmType_Values.BruteForce);

            bool actual = _Sut.IsPrime(number);

            Assert.IsTrue(actual, $"{number} is a prime number");
        }
Esempio n. 9
0
        public void IsPrime_ShouldReturnFalse_IfNumberIsFour()
        {
            _Sut = new PrimeService(PrimeAlgorithmType_Values.BruteForce);

            bool actual = _Sut.IsPrime(4);

            Assert.IsFalse(actual, "4 is not a prime number");
        }
Esempio n. 10
0
        public override string GetAnswer()
        {
            const int lowerLimit = 56995; // The example given in the question is 56**3, so that can't be the answer.

            for (int i = lowerLimit; ; i += 2)
            {
                if (!_primeService.IsPrime(i))
                {
                    continue;
                }

                if (FitsRule(i))
                {
                    return(i.ToString());
                }
            }
        }
Esempio n. 11
0
        public void IsPrime_ShouldReturnTrue_IfNumberIsThree()
        {
            _Sut = new PrimeService(PrimeAlgorithmType_Values.SieveOfEratosthenes);

            bool actual = _Sut.IsPrime(3);

            Assert.IsTrue(actual, "3 is a prime number");
        }
Esempio n. 12
0
        public void IsPrime_ShouldReturnFalse_IfNumberIsOne()
        {
            _Sut = new PrimeService(PrimeAlgorithmType_Values.SieveOfEratosthenes);

            bool actual = _Sut.IsPrime(1);

            Assert.IsFalse(actual, "1 is not a prime number");
        }
Esempio n. 13
0
        public override string GetAnswer()
        {
            // This works for 1487, given in the example, so starting at 1489.
            for (int i = 1489; i < 10000; i += 2)
            {
                if (StringUtilities.IsPermutation(i.ToString(), (i + 3330).ToString()) &&
                    StringUtilities.IsPermutation(i.ToString(), (i + 6660).ToString()) &&
                    _primeService.IsPrime(i) &&
                    _primeService.IsPrime(i + 3330) &&
                    _primeService.IsPrime(i + 6660))
                {
                    return(i.ToString() + (i + 3330) + (i + 6660));
                }
            }

            return("not found");
        }
Esempio n. 14
0
        public override string GetAnswer()
        {
            // Let's assume that the number of consecutive primes < b.
            int bestA = 1;
            int bestB = 41;
            int consecutivePrimeCount = 40;

            for (int b = 1000; b >= -1000; --b)
            {
                if (!_primeService.IsPrime(b))
                {
                    continue;
                }

                for (int a = -1000; a < 1000; ++a)
                {
                    if (!_primeService.IsPrime(1 + a + b))
                    {
                        continue;
                    }

                    // Check length of consecutive prime list.
                    int n = 2;
                    while (_primeService.IsPrime((n * n) + (a * n) + b))
                    {
                        ++n;
                    }

                    if (n > consecutivePrimeCount)
                    {
                        consecutivePrimeCount = n;

                        bestA = a;
                        bestB = b;
                    }
                }
            }

            return((bestA * bestB).ToString());
        }
Esempio n. 15
0
        public override string GetAnswer()
        {
            int count = 0;
            int sum   = 0;

            // 2, 3, 5, 7 not considered truncatable, so start at 11.
            for (int i = 11; count < 11; i += 2)
            {
                if (!HasOddDigits(i) || !PrimeEndDigits(i) || !_primeService.IsPrime(i))
                {
                    continue;
                }

                if (IsTruncatable(i))
                {
                    ++count;
                    sum += i;
                }
            }

            return(sum.ToString());
        }
Esempio n. 16
0
 public bool IsPrime(int number)
 {
     return(_Inner.IsPrime(number));
 }
Esempio n. 17
0
        public void IsPrime_InputIs1_ReturnFalse()
        {
            var result = _primeService.IsPrime(1);

            Assert.False(result, "1 should not be prime");
        }