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
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.util.ArrayList;
import java.util.ListIterator;
/**
* Created by IntelliJ IDEA.
* User: monkegoist
* Date: 3/8/11
* Time: 9:58 AM
*/
public class Get100 {
public static void main(String[] args) throws Exception {
char[][] ops = new char[Integer.parseInt("44444444",5)][8];
// Array of possible options
StringBuilder sb;
int l;
for (int i = 0; i < Integer.parseInt("44444444",5); i++) {
sb = new StringBuilder(Integer.toString(i+1,5));
l = sb.toString().length();
for (int k = 0; k < 8 - l; k++) {
sb.insert(0,'0');
}
// System.out.println("sb = " + sb);
ops[i] = sb.toString().toCharArray();
}
// This list will contain options that evaluate to 100
ArrayList<Integer> results = new ArrayList<Integer>();
// engine to evaluate expressions
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
Integer lookingFor = new Integer(100);
// Processing...
for (int i = 0; i < Integer.parseInt("44444444",5); i++) {
StringBuilder expression = new StringBuilder("1");
for (int k = 1; k < 10; k++) {
int next = k + 1;
if (k < 9) {
switch(ops[i][k-1]) {
case '0': expression.append(next); break;
case '1': expression.append("+" + next); break;
case '2': expression.append("-" + next); break;
case '3': expression.append("*" + next); break;
case '4': expression.append("/" + next); break;
}
}
}
Double val = (Double)engine.eval(expression.toString());
if (val.equals(lookingFor.doubleValue())) {
results.add(i);
}
}
System.out.println(results.size() + " were found:");
ListIterator<Integer> iter = results.listIterator();
while (iter.hasNext()) {
System.out.println(printMe(ops[iter.next()]) + " = 100");
}
}
static String printMe(char[] x) {
StringBuilder path = new StringBuilder("");
for (int i = 1; i < 10; i++) {
path.append(i);
if (i < 9) {
switch(x[i-1]) {
case '0': break;
case '1': path.append("+"); break;
case '2': path.append("-"); break;
case '3': path.append("*"); break;
case '4': path.append("/"); break;
}
}
}
return path.toString();
}
}