From problem #119 of the Project Euler :
The number 512 is interesting because it is equal to the sum of its digits raised to some power: 5 + 1 + 2 = 8, and 83 = 512. Another example of a number with this property is 614656 = 284.
We shall define an to be the nth term of this sequence and insist that a number must contain at least two digits to have a sum.
You are given that a2 = 512 and a10 = 614656.
Find a30.
The number 512 fits because :
5+1+2 = 8 and 83 = 512.
The number 17576 fits because :
1+7+5+7+6 = 26 and 263 = 17576.
There's my solution but it's extremely inefficient. I need the variable c to reach 25 but I haven't seen past 16.
public static int c = 0;
public static void TryAll(long x, long y)
{
for (int i = 2; i < 10; i++)
{
double powered = Math.Pow(y, i);
if (x % y == 0 && powered == x && x % 10 != 0)
{
c++;
Console.WriteLine("----- {0}", c);
Console.WriteLine(powered);
Console.WriteLine(y);
Console.WriteLine(i);
}
}
}
public static void Main(string[] args)
{
int baseNum = 0;
for (c = c; c < 26; baseNum++)
{
if (baseNum > 9)
{
int sum = 0;
int baseNumD = baseNum;
while (baseNumD != 0)
{
sum += baseNumD % 10;
baseNumD /= 10;
}
TryAll(baseNum, sum);
}
}
}