-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem30.java
44 lines (37 loc) · 994 Bytes
/
Problem30.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.javamultiplex.projecteuler;
public class Problem30 {
public static void main(String[] args) {
int start = 2;
/*
* Upper bound is 355000 because largest number that is generated by 6
* digits is 6*9^5 = 354294 so we can take more upper bound as 355000.
*/
int end = 355000;
int sum = 0;
for (int i = start; i <= end; i++) {
if (canBeWrittenAsFifithPowerSum(i)) {
sum += i;
}
}
System.out.println(sum);
}
private static boolean canBeWrittenAsFifithPowerSum(int i) {
// Converting int to String.
String num = String.valueOf(i);
String result = null;
boolean valid = false;
int length = num.length();
int temp = 0, sum = 0;
for (int j = 0; j < length; j++) {
// Converting char to int.
temp = num.charAt(j) - 48;
sum += Math.pow(temp, 5);
}
// Converting int to String.
result = String.valueOf(sum);
if (result.equals(num)) {
valid = true;
}
return valid;
}
}