http://projecteuler.net/index.php?section=problems&id=37
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.
Find the sum of the only eleven primes that are both truncatable from left to right and right to left.
NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
______
so the first thing I did wrong was to consider 1 to be a prime.. once I’d sorted that out I got this list.. which is 11 primes… so those must be what they’re after!
23
37
53
73
313
317
373
797
3137
3797
739397
_________________
static void Main(string[] args)
{
long sum = 0;
int found = 0;
long i = 11;
while (found<11)
{
int c=0;
i++;
int l = i.ToString().Length;
for (int x = 0; x < l; x++)
{
long t1 = Convert.ToInt64(i.ToString().Substring(x, l - x));
long t2 = Convert.ToInt64(i.ToString().Substring(0, x+1));
if (isprime(t1) && isprime(t2) && t1 != 1 && t2 != 1 )
{
c++;
}
else
{
c = 0;
}
}
if (c == i.ToString().Length)
{
sum += i;
found++;
Console.WriteLine(i);
}
}
Console.WriteLine("Answer>>>"+sum);
Console.ReadLine();
}
static bool isprime(long n)
{
if (n == 2 || n == 3 || n == 5) { return true; }
if (n%2==0) {return false;}
if (n%3 == 0) { return false;}
if (n%5 == 0) { return false;}
for (int t = 7; t < Math.Sqrt(n); t++)
{
if (n % t== 0)
{
return false;
}
}
return true;
}
No comments:
Post a Comment