Project Euler #30: Digit Nth powers

  • + 0 comments

    //C# using System;

    class Program { static int PowerSum(int num, int power) { int sum = 0; int temp = num;

        while (temp > 0)
        {
            int digit = temp % 10;
            sum += (int)Math.Pow(digit, power);
            temp /= 10;
        }
    
        return sum;
    }
    
    static void Main()
    {
        int N = int.Parse(Console.ReadLine());
        int totalSum = 0;
    
        for (int i = 10; i <= 999999; i++)
        {
            if (i == PowerSum(i, N))
            {
                totalSum += i;
            }
        }
    
        Console.WriteLine(totalSum);
    }
    

    }