forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp032.java
More file actions
56 lines (45 loc) · 1.29 KB
/
p032.java
File metadata and controls
56 lines (45 loc) · 1.29 KB
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
48
49
50
51
52
53
54
55
56
/*
* Solution to Project Euler problem 32
* 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 p032 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p032().run());
}
/*
* For contradiction suppose a candidate (x, y, z) has z >= 10000.
* Then x*y consumes at least 5 digits. With the 4 (or fewer)
* remaining digits, even the upper bound of x=99 and y=99
* produces a product of x*y < 10000, which is unequal to z.
*
* Therefore we need the product z < 10000 to be able to find
* possible x and y values.
*/
public String run() {
int sum = 0;
for (int i = 1; i < 10000; i++) {
if (hasPandigitalProduct(i))
sum += i;
}
return Integer.toString(sum);
}
private static boolean hasPandigitalProduct(int n) {
// Find and examine all factors of n
for (int i = 1; i <= n; i++) {
if (n % i == 0 && isPandigital("" + n + i + n/i))
return true;
}
return false;
}
private static boolean isPandigital(String s) {
if (s.length() != 9)
return false;
char[] temp = s.toCharArray();
Arrays.sort(temp);
return new String(temp).equals("123456789");
}
}