Talk:Addition-chain exponentiation: Difference between revisions

→‎Inefficient chain finding: reference lengths in case useful
(→‎Inefficient chain finding: reference lengths in case useful)
Line 12:
 
== Inefficient chain finding ==
As a reference to optimal chain length. Optimal addition chain is NP-complete. In other words, don't use the following for big numbers where "big" is "larger than a few tens, probably 20".
<lang python>defmaxn chain(n, l=(1,)): 200
 
def expo(r = [0] + [100000] * maxn, s = (1,)):
Optimal addition chain is NP-complete. In other words, don't use the following for big numbers where "big" is "larger than a few tens, probably 20".
l, f = len(s), s[0]
<lang python>def chain(n, l=(1,)):
if f < len(r) and l - 1 <= r[f]:
r[f] = l - 1
for i in s:
expo(r, (f + i,) + s)
return r
 
def chain(n, l=(1,)):
if n in l: return [l]
 
Line 30 ⟶ 39:
return r
 
for i in range(20): print(i, chain(i))</lang>
 
r = expo()
for i in range(maxn): print(i, r[i])</lang>
Anonymous user