measured with timeit.py (fastest of 3 repeats):

Python 2.7.1:

n 8 9 10 11 12 13 14

invol1 0.003 0.010 0.040 0.165 0.673 2.844 12.321

invol2 0.002 0.009 0.034 0.130 0.516 2.114 9.116

invol3 0.002 0.007 0.027 0.102 0.405 1.633 7.016

Python 3.1:

n 8 9 10 11 12 13 14

invol1 0.003 0.012 0.048 0.198 0.798 3.338 14.470

invol2 0.003 0.009 0.034 0.131 0.514 2.107 9.070

invol3 0.002 0.007 0.026 0.100 0.399 1.624 7.029

“invol1” and “invol2” are the 2 versions that I posted above. “invol3”

is a further optimization to invol2: I replaced the inner loop body

x[x.index(i)] += 1

tmp.append(x[:i-1] + [k] + x[i-1:] + [i])

by

a = x[i-1]; x[a-1-(a>i)] += 1

w = x[:]

w.insert(i-1, k)

w.append(i)

tmp.append(w)

How does it work: on each iteration of the outer loop s1 contains all the involutions of length k-2, and s2 contains the involutions of length k-1. The involutions of length k are obtained from these:

– take each element of s2 and stick k to the end: that’s an involution of length k that leaves k at its own position.

– the other involutions of length k have the value k swapped with some earlier value i. The remaining k-2 elements are an involution of length k-2 (and we happen to have all those in s1), except the elements must be re-labeled since the value i was taken. The “+=1” or “+(p>i)” stuff in the code handles the re-labeling (try with pencil and paper e.g. with k=5).

If you can have a look at it, it would be great. Thanks! ]]>

def invol2(n):

s1, s2 = ([[1]], [[1,2], [2,1]])

if n < 2:

return s1

for k in range(3,n+1):

tmp = [x+[k] for x in s2]

for x in s1:

tmp.append(x + [k,k-1])

for i in range(k-2,0,-1):

x[x.index(i)] += 1

tmp.append(x[:i-1] + [k] + x[i-1:] + [i])

s1, s2 = s2, tmp

return s2

In case something eats the whitespace, here's a link: http://pastebin.com/tbrDjWKg

]]>From the comment above when I saw finally that these numbers really exists in math, I searched for other solutions too. I will update the post with your solution and that other one I found, although your code > the one I found > my code. So my solution is the slowest. ]]>

from the recurrence: http://pastebin.com/U6qz7P6V

Timings:

length = 8, best of 5 runs of invol: 0.005 s

length = 9, best of 5 runs of invol: 0.020 s

length = 10, best of 5 runs of invol: 0.081 s

length = 11, best of 5 runs of invol: 0.331 s

length = 8, best of 5 runs of get_mirrors_recursively_v2: 0.028 s

length = 9, best of 5 runs of get_mirrors_recursively_v2: 0.101 s

length = 10, best of 5 runs of get_mirrors_recursively_v2: 0.371 s

length = 11, best of 5 runs of get_mirrors_recursively_v2: 1.421 s