Project Euler #30: Digit Nth powers

  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt(); // Read the input
    
        long sum = 0; // Variable to store the sum
    
        for (int i = 10; i <= 999999; i++) { 
            if (isDigitPowerSum(i, n)) { 
                sum += i; 
            }
        }
    
        System.out.println(sum);
    }
    public static boolean isDigitPowerSum(int num, int n) {
    
        int sum = 0 ;
        int orignal= num;
        while(num>0){
            int digit = num % 10;
            sum += Math.pow(digit, n);
            num /= 10;
        }
        return sum==orignal;
    
    }
    

    }