sum of all the numbers that can be written as the sum of fifth powers of their digits

Mar 21, 2013 at 6:58am
Hi everyone. I am new to C++ and I am working on project Euler problem 30 and running into some problems. The problem asks you to find the sum of all the numbers that can be written as the sum of fifth powers of their digits. I think I am on the right track with my code but for some reason it is not returning any numbers. If anyone could take a look at my code and let me know my mistake I would really appreciate it. Thanks for the help!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    unsigned long num = 0, tot = 0, final = 0;
    int digit = 0;

    for(unsigned long a = 2; a < 4000000; a++)
    {
        num = a;
        while (num)
        {
           digit = num % 10;
            num /= 10;
            tot += pow(digit, 5);
        }
        if (a == tot)
            final += a;
    }
    cout << final << endl;
    return 0;
}
Mar 21, 2013 at 10:39pm
You forgot to reset tot to zero for each new value of a.
Topic archived. No new replies allowed.