Project Euler #119 : Digit power sum 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 &lt; 10; i++)
        {
            double powered = Math.Pow(y, i);
            if (x % y == 0 &amp;&amp; powered == x &amp;&amp; x % 10 != 0)
            {
                c++;
                Console.WriteLine(&quot;----- {0}&quot;, c);
                Console.WriteLine(powered);
                Console.WriteLine(y);
                Console.WriteLine(i);
            }
        }
    }
    public static void Main(string[] args)
    {
        int baseNum = 0;
        for (c = c; c &lt; 26; baseNum++)
        {

            if (baseNum &gt; 9)
            {
                int sum = 0;
                int baseNumD = baseNum;
                while (baseNumD != 0)
                {
                    sum += baseNumD % 10;
                    baseNumD /= 10;
                }
                TryAll(baseNum, sum);
            }
        }
    }
If you don't need to print out results in order, you can inverse the logic and compute power value first, and check if the result equals to sum of digits. It's super fast:

using System;

public class Program
{
    private const int MaxCount = 25;

    public static void Main(string[] args)
    {
        var c = 1;
        for (var i = 2; i &lt; 100; i++)
        {
            for (var j = 2; j &lt; 1000; j++)
            {
                var pow = (long)Math.Pow(j, i);
                var sum = SumOfDigits(pow);
                if (sum != j)
                {
                    continue;
                }
                Console.WriteLine($"{c}: {pow} = {sum} ^ {i}");
                c++;
                if (c &gt; MaxCount)
                {
                    break;
                }
            }
            if (c &gt; MaxCount)
            {
                break;
            }
        }
        Console.ReadLine();
    }

    private static long SumOfDigits(long value)
    {
        long sum = 0;
        while (value != 0)
        {
            sum = sum + (value % 10); 
            value = value / 10; 
        }

        return sum;
    }
}


Please note loop limit values are arbitrary, large enough to satisfy requirement of 25 results.