Project Euler #30: Digit Nth powers

  • + 0 comments

    C++

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
         
        int n;
        cin >> n;
        vector<int> arr(10);
        for (int i=0; i<=9; i++)
        {
            int p = pow(i, n);
            arr[i] = p;
        }
        int S = 0;
        for (int i=100; i<1000000; i++)
        {
            int num = i, sum=0;
            while(num>0)
            {
                int z = num%10;
                sum = sum+arr[z];
                num = num/10;
            }
            if (sum==i) S = S + sum;
        }
        cout << S;
        return 0;
    }