Knuth's power tree: Difference between revisions

Content added Content deleted
No edit summary
(Added 11l)
Line 78: Line 78:
::*   link to Rosetta Code   [http://rosettacode.org/wiki/Addition-chain_exponentiation addition-chain exponentiation].
::*   link to Rosetta Code   [http://rosettacode.org/wiki/Addition-chain_exponentiation addition-chain exponentiation].
<br><br>
<br><br>

=={{header|11l}}==
{{trans|Python}}

<lang 11l>V p = [1 = 0]
V lvl = [[1]]

F path(n)
I !n
R [Int]()
L n !C :p
[Int] q
L(x) :lvl[0]
L(y) path(x)
I !(x + y C :p)
:p[x + y] = x
q.append(x + y)
:lvl[0] = q

R path(:p[n]) [+] [n]

F tree_pow_i(x, n)
V (r, p) = ([0 = BigInt(1), 1 = BigInt(x)], 0)
L(i) path(n)
r[i] = r[i - p] * r[p]
p = i
R r[n]

F tree_pow_f(x, n)
V (r, p) = ([0 = 1.0, 1 = x], 0)
L(i) path(n)
r[i] = r[i - p] * r[p]
p = i
R r[n]

F show_pow_i(x, n)
print("#.: #.\n#.^#. = #.\n".format(n, path(n), x, n, tree_pow_i(x, n)))

F show_pow_f(x, n)
print("#.: #.\n#.^#. = #.6\n".format(n, path(n), x, n, tree_pow_f(x, n)))

L(x) 18
show_pow_i(2, x)
show_pow_i(3, 191)
show_pow_f(1.1, 81)</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, 3, 5]
2^5 = 32

6: [1, 2, 3, 6]
2^6 = 64

7: [1, 2, 3, 5, 7]
2^7 = 128

8: [1, 2, 4, 8]
2^8 = 256

9: [1, 2, 3, 6, 9]
2^9 = 512

10: [1, 2, 3, 5, 10]
2^10 = 1024

11: [1, 2, 3, 5, 10, 11]
2^11 = 2048

12: [1, 2, 3, 6, 12]
2^12 = 4096

13: [1, 2, 3, 5, 10, 13]
2^13 = 8192

14: [1, 2, 3, 5, 7, 14]
2^14 = 16384

15: [1, 2, 3, 5, 10, 15]
2^15 = 32768

16: [1, 2, 4, 8, 16]
2^16 = 65536

17: [1, 2, 4, 8, 16, 17]
2^17 = 131072

191: [1, 2, 3, 5, 7, 14, 19, 38, 57, 95, 190, 191]
3^191 = 13494588674281093803728157396523884917402502294030101914066705367021922008906273586058258347

81: [1, 2, 3, 5, 10, 20, 40, 41, 81]
1.1^81 = 2253.240236
</pre>


=={{header|EchoLisp}}==
=={{header|EchoLisp}}==