forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp093.java
100 lines (89 loc) · 2.63 KB
/
p093.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Solution to Project Euler problem 93
* By Nayuki Minase
*
* http://nayuki.eigenstate.org/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
import java.math.BigInteger;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;
public final class p093 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p093().run());
}
public String run() {
int longest = 0;
int abcd = -1;
for (int a = 1; a <= 9; a++) {
for (int b = a + 1; b <= 9; b++) {
for (int c = b + 1; c <= 9; c++) {
for (int d = c + 1; d <= 9; d++) {
int consec = longestConsecutive(a, b, c, d);
if (consec > longest) {
longest = consec;
abcd = a * 1000 + b * 100 + c * 10 + d;
}
}
}
}
}
return Integer.toString(abcd);
}
// Assumes 1 <= a < b < c < d <= 9
private static int longestConsecutive(int a, int b, int c, int d) {
Set<Integer> expressible = new HashSet<Integer>();
// Try all possible orderings of operands and operators
int[] ops = {0, 0, 0, a, b, c, d}; // 0 = operator slot, 1 to 9 = literal operand
outer:
do {
// Try all possibilities for the 3 operators
inner:
for (int i = 0; i < 64; i++) {
Stack<Fraction> stack = new Stack<Fraction>();
int j = 0; // Operator index
for (int op : ops) {
if (1 <= op && op <= 9) { // Operand
stack.push(new Fraction(BigInteger.valueOf(op), BigInteger.ONE));
} else if (op == 0) { // Operator
if (stack.size() < 2)
continue outer; // Stack underflow; skip this ordering
Fraction right = stack.pop();
Fraction left = stack.pop();
switch ((i >>> (j * 2)) & 3) {
case 0:
stack.push(left.add(right));
break;
case 1:
stack.push(left.subtract(right));
break;
case 2:
stack.push(left.multiply(right));
break;
case 3:
if (right.numerator.signum() == 0)
continue inner; // Division by zero; skip the result for this case
stack.push(left.divide(right));
break;
default:
throw new AssertionError();
}
j++; // Consume an operator
} else
throw new AssertionError();
}
if (stack.size() != 1)
throw new AssertionError();
Fraction result = stack.pop();
if (result.denominator.equals(BigInteger.ONE))
expressible.add(result.numerator.intValue());
}
} while (Library.nextPermutation(ops));
// Find largest set of consecutive expressible integers starting from 1
for (int i = 0; ; i++) {
if (!expressible.contains(i + 1))
return i;
}
}
}