-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem037.cs
81 lines (69 loc) · 1.82 KB
/
Problem037.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
namespace ProjectEuler.Solutions
{
using System;
using System.Collections.Generic;
using NUnit.Framework;
using ProjectEuler.Helper;
/// <summary>
/// Truncatable primes.
/// The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7.
/// Similarly we can work from right to left: 3797, 379, 37, and 3.
/// <para>Find the sum of the only eleven primes that are both truncatable from left to right and right to left.</para>
/// <b>NOTE</b>: 2, 3, 5, and 7 are not considered to be truncatable primes.
/// </summary>
public class Problem037 : Problem
{
public override long Solution()
{
List<long> truncatablePrimes = new List<long>();
foreach(long prime in Primes.GetPrimeNumber())
{
if(IsTruncatablePrime(prime))
{
truncatablePrimes.Add(prime);
}
if(truncatablePrimes.Count == 11)
{
break;
}
}
long sum = 0;
foreach(long truncatablePrime in truncatablePrimes)
{
sum += truncatablePrime;
}
return sum;
}
private static bool IsTruncatablePrime(long prime)
{
long leftPrime = prime;
long rightPrime = prime;
string primeString = prime.ToString();
int length = primeString.Length;
bool isTruncatable = length > 1;
int upperFactor = (int)Math.Pow(10, length - 1);
while(upperFactor > 1)
{
leftPrime %= upperFactor;
rightPrime /= 10;
if(!Primes.IsPrimeNumber(leftPrime) || !Primes.IsPrimeNumber(rightPrime))
{
isTruncatable = false;
break;
}
upperFactor /= 10;
}
return isTruncatable;
}
[Test]
public void TestForExample()
{
Assert.IsTrue(IsTruncatablePrime(3797));
}
[Test]
public void TestForProblem()
{
Assert.AreEqual(748317, this.Solution());
}
}
}