forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp052.java
More file actions
44 lines (33 loc) · 938 Bytes
/
p052.java
File metadata and controls
44 lines (33 loc) · 938 Bytes
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
/*
* Solution to Project Euler problem 52
* Copyright (c) Project Nayuki. All rights reserved.
*
* https://www.nayuki.io/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
import java.util.Arrays;
public final class p052 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p052().run());
}
public String run() {
for (int i = 1; ; i++) {
if (i > Integer.MAX_VALUE / 6)
throw new ArithmeticException("Overflow");
if (multiplesHaveSameDigits(i))
return Integer.toString(i);
}
}
private static boolean multiplesHaveSameDigits(int x) {
for (int i = 2; i <= 6; i++) {
if (!Arrays.equals(toSortedDigits(x), toSortedDigits(i * x)))
return false;
}
return true;
}
private static char[] toSortedDigits(int x) {
char[] result = Integer.toString(x).toCharArray();
Arrays.sort(result);
return result;
}
}