We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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;
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #30: Digit Nth powers
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.;
public class Solution {
}