Problem 30: Distinct Powers

Problem 30
Turned integer into a string, took each character and did the operations added the powers that way. Then I just checked the numbers if they were equal.


/**
Surprisingly there are only
 three numbers that can be written
  as the sum of fourth powers of their digits:

1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44
As 1 = 14 is not a sum it is not included.

The sum of these numbers is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers 
that can be written as the sum of fifth powers of their digits.
 */
import java.util.*;
public class problem29 {
    
    public static boolean isDigitFifth(int num){
    	String strNum = Integer.toString(num);
    	int sum = 0;
    	for(int i = 0; i < strNum.length(); i++)
    	{
    		sum += Math.pow(
    			Integer.parseInt(
    				Character.toString(
    					strNum.charAt(i))),5);
    	}
    	if(sum == num)
    		return true;
    	else return false;
    }
    public static void main(String[] args) {
    	int sum = 0;
        for(int i = 2; i < 1000000; i++)
        {
        	if(isDigitFifth(i)){
        		sum += i;
        		System.out.println(i);
        	}       		
        }
        System.out.println(sum);
    }
}



Output
——————–Configuration: ——————–
4150
4151
54748
92727
93084
194979
443839

Process completed.

Leave a Reply