ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software

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

Updated on April 29, 2009
// 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;
    }


Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article