-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEuler62.java
47 lines (34 loc) · 948 Bytes
/
Euler62.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
45
46
47
import java.util.*;
class Euler62 {
public static void main(String[] arg) {
for(long a = 5014; a <= 6000; a++) {
System.err.println(a);
long n = a*a*a;
String next = "" + n;
char[] arr = next.toCharArray();
Set<Long> set = new TreeSet<Long>();
set.add(Long.parseLong(new String(arr)));
Euler24.next(arr, arr.length - 1);
while(!next.equals(new String(arr))) {
next = new String(arr);
long permutation = Long.parseLong(next);
long cbrt = isPerfectCube(permutation);
if(cbrt > 0) {
set.add(permutation);
}
Euler24.next(arr, arr.length - 1);
}
if(set.size() == 5) {
System.out.println(n);
break;
}
}
}
public static long isPerfectCube(long n) {
long cbrt = Math.round(Math.pow(n, 1.0/3));
if(cbrt*cbrt*cbrt == n)
return cbrt;
else
return -1;
}
}