Example #1
0
        //http://en.wikipedia.org/wiki/Truncatable_prime
        public override long Solve()
        {
            long sum = 0;
              var primes = Primes.CalculatePrimesBelow(1000000);

              foreach(var prime in primes) {
            bool truncatable = true;
            if (PossibleTruncatablePrime(prime)) {
              var leftToRightTruncate = new Truncate(prime, TruncateMethod.LeftToRight);
              while (leftToRightTruncate.CanTruncate()) {
            if (!primes.Contains(leftToRightTruncate.NextValue())) {
              truncatable = false;
              break;
            }
              }

              var rightToLeftTruncate = new Truncate(prime, TruncateMethod.RightToLeft);
              while (rightToLeftTruncate.CanTruncate()) {
            if (!primes.Contains(rightToLeftTruncate.NextValue())) {
              truncatable = false;
              break;
            }
              }
              if (truncatable) sum += prime;
            }
              }
              return sum;
        }
Example #2
0
 public void Truncate_Right_To_Left()
 {
     var truncate = new Truncate(345, TruncateMethod.RightToLeft);
       Assert.IsTrue(truncate.CanTruncate());
       Assert.AreEqual(34, truncate.NextValue());
       Assert.IsTrue(truncate.CanTruncate());
       Assert.AreEqual(3, truncate.NextValue());
       Assert.IsFalse(truncate.CanTruncate());
       Assert.AreEqual(3, truncate.CurrentValue());
 }
Example #3
0
 public void Truncate_Left_To_Right()
 {
     var truncate = new Truncate(234, TruncateMethod.LeftToRight);
       Assert.IsTrue(truncate.CanTruncate());
       Assert.AreEqual(34, truncate.NextValue());
       Assert.IsTrue(truncate.CanTruncate());
       Assert.AreEqual(4, truncate.NextValue());
       Assert.IsFalse(truncate.CanTruncate());
       Assert.AreEqual(4, truncate.CurrentValue());
 }
Example #4
0
        //http://en.wikipedia.org/wiki/Truncatable_prime

        public override long Solve()
        {
            long sum    = 0;
            var  primes = Primes.CalculatePrimesBelow(1000000);

            foreach (var prime in primes)
            {
                bool truncatable = true;
                if (PossibleTruncatablePrime(prime))
                {
                    var leftToRightTruncate = new Truncate(prime, TruncateMethod.LeftToRight);
                    while (leftToRightTruncate.CanTruncate())
                    {
                        if (!primes.Contains(leftToRightTruncate.NextValue()))
                        {
                            truncatable = false;
                            break;
                        }
                    }

                    var rightToLeftTruncate = new Truncate(prime, TruncateMethod.RightToLeft);
                    while (rightToLeftTruncate.CanTruncate())
                    {
                        if (!primes.Contains(rightToLeftTruncate.NextValue()))
                        {
                            truncatable = false;
                            break;
                        }
                    }
                    if (truncatable)
                    {
                        sum += prime;
                    }
                }
            }
            return(sum);
        }