Project Euler problem 37 says:
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.
Going right to left is trivial enough as you just keep dividing by 10:
IEnumerable<long> RTL (long x)
{
while(x > 10) {
x /= 10;
yield return x;
}
}
Going left to right was a little trickier. Just wondering if there's a more optimal way to do this.
IEnumerable<long> LTR (long x)
{
while(x > 10)
{
long y = x; //take a copy of current value of x;
int powerten = 0; //variable to count what the 10^n value is
while(y > 10){ //calculate powerten by repeated dividing y / 10
y /= 10; // for 3797 this would be '3' i.e. 3.797 * 10^3
powerten++;
}
long p = (long)Math.Pow(10, powerten); // 10 ^ 3 = 1000
long z = (x/p)*p; // 3797/1000*1000 = 3000
x = (x - z); // subtract from x
yield return x;
}
}
It runs pretty fast but I'm just wondering is there a more optimal way to remove the leftmost significant digit of a number on each iteration without all the "gymnastics".