Calculate numbers that are equal to the sum of the fifth powers of their digits

// Calculate numbers that are equal to the sum of their digits to the 5th power.
// This will run for a very long time as written.
// An upper bound would be useful, but deciding on that value is problematic.
    public static long Solve() {
        long result = 0;
        long num = 2, tmp, sum;
        int digit;
        try {
            while (num > 0) {
                tmp = num;
                sum = 0;
                while (tmp != 0) {
                    digit = (int) tmp % 10;
                    sum = sum + digit * digit * digit * digit * digit;
                    tmp /= 10;
                }
                if (sum == num) {
                    System.out.println("Number found: " + sum);
                    result += sum;
                }
                num++;
            }
        } catch (Exception ex) {
            System.out.println("Solve(): " + ex.getMessage());
        }
        return result;
    }


More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working