Topswops: Difference between revisions

no edit summary
(→‎{{header|Haskell}}: added alternate version)
No edit summary
Line 511:
9: 30
10: 38</pre>
 
=={{header|Julia}}==
Brute force method
<lang julia>function topswops(n)
first = [1:n]; swapsa = ref(Int)
for perm = 2:(factorial(n)/factorial(n-n))
swaps = 0
a = nthperm(first,perm)
if a == first
break
else
while a[1] != 1
a[1:a[1]] = reverse(a[1:a[1]]); swaps+= 1
end
push!(swapsa,swaps)
end
end
return max(swapsa)
end</lang>
<pre>julia> for i = 1:10 println(topswops(i)) end
0
1
2
4
7
10
16
22
30
38</pre>
 
=={{header|Mathematica}}==
An exhaustive search of all possible permutations is done