Knuth's power tree: Difference between revisions
Content added Content deleted
m (used a lowercase x instead of a capital X in the task's preamble.) |
|||
Line 328: | Line 328: | ||
3 ^ 191 = 13494588674281093803728157396523884917402502294030101914066705367021922008906273586058258347 |
3 ^ 191 = 13494588674281093803728157396523884917402502294030101914066705367021922008906273586058258347 |
||
</pre> |
</pre> |
||
=={{header|Groovy}}== |
|||
{{trans|Java}} |
|||
<lang groovy>class PowerTree { |
|||
private static Map<Integer, Integer> p = new HashMap<>() |
|||
private static List<List<Integer>> lvl = new ArrayList<>() |
|||
static { |
|||
p[1] = 0 |
|||
List<Integer> temp = new ArrayList<Integer>() |
|||
temp.add 1 |
|||
lvl.add temp |
|||
} |
|||
private static List<Integer> path(int n) { |
|||
if (n == 0) return new ArrayList<Integer>() |
|||
while (!p.containsKey(n)) { |
|||
List<Integer> q = new ArrayList<>() |
|||
for (Integer x in lvl.get(0)) { |
|||
for (Integer y in path(x)) { |
|||
if (p.containsKey(x + y)) break |
|||
p[x + y] = x |
|||
q.add x + y |
|||
} |
|||
} |
|||
lvl[0].clear() |
|||
lvl[0].addAll q |
|||
} |
|||
List<Integer> temp = path p[n] |
|||
temp.add n |
|||
temp |
|||
} |
|||
private static BigDecimal treePow(double x, int n) { |
|||
Map<Integer, BigDecimal> r = new HashMap<>() |
|||
r[0] = BigDecimal.ONE |
|||
r[1] = BigDecimal.valueOf(x) |
|||
int p = 0 |
|||
for (Integer i in path(n)) { |
|||
r[i] = r[i - p] * r[p] |
|||
p = i |
|||
} |
|||
r[n] |
|||
} |
|||
private static void showPos(double x, int n, boolean isIntegral) { |
|||
printf("%d: %s\n", n, path(n)) |
|||
String f = isIntegral ? "%.0f" : "%f" |
|||
printf(f, x) |
|||
printf(" ^ %d = ", n) |
|||
printf(f, treePow(x, n)) |
|||
println() |
|||
println() |
|||
} |
|||
static void main(String[] args) { |
|||
for (int n = 0; n <= 17; ++n) { |
|||
showPos 2.0, n, true |
|||
} |
|||
showPos 1.1, 81, false |
|||
showPos 3.0, 191, true |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre>0: [] |
|||
2 ^ 0 = 1 |
|||
1: [1] |
|||
2 ^ 1 = 2 |
|||
2: [1, 2] |
|||
2 ^ 2 = 4 |
|||
3: [1, 2, 3] |
|||
2 ^ 3 = 8 |
|||
4: [1, 2, 4] |
|||
2 ^ 4 = 16 |
|||
5: [1, 2, 4, 5] |
|||
2 ^ 5 = 32 |
|||
6: [1, 2, 4, 6] |
|||
2 ^ 6 = 64 |
|||
7: [1, 2, 4, 6, 7] |
|||
2 ^ 7 = 128 |
|||
8: [1, 2, 4, 8] |
|||
2 ^ 8 = 256 |
|||
9: [1, 2, 4, 8, 9] |
|||
2 ^ 9 = 512 |
|||
10: [1, 2, 4, 8, 10] |
|||
2 ^ 10 = 1024 |
|||
11: [1, 2, 4, 8, 10, 11] |
|||
2 ^ 11 = 2048 |
|||
12: [1, 2, 4, 8, 12] |
|||
2 ^ 12 = 4096 |
|||
13: [1, 2, 4, 8, 12, 13] |
|||
2 ^ 13 = 8192 |
|||
14: [1, 2, 4, 8, 12, 14] |
|||
2 ^ 14 = 16384 |
|||
15: [1, 2, 4, 8, 12, 14, 15] |
|||
2 ^ 15 = 32768 |
|||
16: [1, 2, 4, 8, 16] |
|||
2 ^ 16 = 65536 |
|||
17: [1, 2, 4, 8, 16, 17] |
|||
2 ^ 17 = 131072 |
|||
81: [1, 2, 4, 8, 16, 32, 64, 80, 81] |
|||
1.100000 ^ 81 = 2253.240236 |
|||
191: [1, 2, 4, 8, 16, 32, 64, 128, 160, 176, 184, 188, 190, 191] |
|||
3 ^ 191 = 13494588674281093803728157396523884917402502294030101914066705367021922008906273586058258347</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |