Factorial base numbers indexing permutations of a collection: Difference between revisions

Content deleted Content added
Petelomax (talk | contribs)
Wherrera (talk | contribs)
Line 322: Line 322:
18.14.25.48.18.9.1.16.15.11.41.8.26.19.36.11.8.21.20.15.15.14.27.10.5.24.0.11.18.12.6.8.5.14.16.10.13.13.9.7.11.1.1.7.0.2.5.0.3.0.0
18.14.25.48.18.9.1.16.15.11.41.8.26.19.36.11.8.21.20.15.15.14.27.10.5.24.0.11.18.12.6.8.5.14.16.10.13.13.9.7.11.1.1.7.0.2.5.0.3.0.0
9♥K♥K♦2♣7♥5♠K♠6♥8♥A♥3♣4♠4♦J♦5♣J♥3♠6♦7♦A♦Q♦2♥7♣10♥8♠8♣A♠10♦Q♣8♦2♠4♥6♠J♣6♣3♦10♣9♣5♦3♥4♣J♠10♠A♣Q♠Q♥K♣9♠2♦7♠5♥9♦
9♥K♥K♦2♣7♥5♠K♠6♥8♥A♥3♣4♠4♦J♦5♣J♥3♠6♦7♦A♦Q♦2♥7♣10♥8♠8♣A♠10♦Q♣8♦2♠4♥6♠J♣6♣3♦10♣9♣5♦3♥4♣J♠10♠A♣Q♠Q♥K♣9♠2♦7♠5♥9♦
</pre>


=={{header|Julia}}==
<lang julia>function makefactorialbased(N, makelist)
listlist = Vector{Vector{Int}}()
count = 0
while true
divisor = 2
makelist && (lis = zeros(Int, N))
k = count
while k > 0
k, r = divrem(k, divisor)
makelist && (divisor <= N + 1) && (lis[N - divisor + 2] = r)
divisor += 1
end
if divisor > N + 2
break
end
count += 1
makelist && push!(listlist, lis)
end
return count, listlist
end

function facmap(factnumbase)
perm = [i for i in 0:length(factnumbase)]
for (n, g) in enumerate(factnumbase)
if g != 0
perm[n:n + g] .= circshift(perm[n:n + g], 1)
end
end
perm
end

function factbasenums()
fcount, factnums = makefactorialbased(3, true)
perms = map(facmap, factnums)
for (i, fn) = enumerate(factnums)
println("$(join(string.(fn), ".")) -> $(join(string(perms[i]), ""))")
end

fcount, _ = makefactorialbased(10, false)
println("\nPermutations generated = $fcount")
println("compared to 11! which = $(factorial(11))\n")

fac51digits = map(s -> [parse(Int, s) for s in split(s, ".")], [
"39.49.7.47.29.30.2.12.10.3.29.37.33.17.12.31.29.34.17.25.2.4.25.4.1.14.20.6.21.18.1.1.1.4.0.5.15.12.4.3.10.10.9.1.6.5.5.3.0.0.0",
"51.48.16.22.3.0.19.34.29.1.36.30.12.32.12.29.30.26.14.21.8.12.1.3.10.4.7.17.6.21.8.12.15.15.13.15.7.3.12.11.9.5.5.6.6.3.4.0.3.2.1"])
perms = map(facmap, fac51digits)

cardshoe = "A♠K♠Q♠J♠T♠9♠8♠7♠6♠5♠4♠3♠2♠A♥K♥Q♥J♥T♥9♥8♥7♥6♥5♥4♥3♥2♥A♦K♦Q♦J♦T♦9♦8♦7♦6♦5♦4♦3♦2♦A♣K♣Q♣J♣T♣9♣8♣7♣6♣5♣4♣3♣2♣"
carr = split(cardshoe, "")
cards = map(x->join(x, ""),[carr[2*i+1] == 'T' ?
["10", carr[i+1][2]] : [carr[2*i+1], carr[2*i+2]] for i in 0:51])
printcardshuffle(c, o) = (println(o); for i in 1:length(o) print(c[o[i] + 1]) end; println())
println("\nTask shuffles:")
for ordering in perms
printcardshuffle(cards, ordering)
end

myrandorder = [rand(collect(0:i)) for i in 51:-1:1]
perm = facmap(myrandorder)
println("\nMy random shuffle:")
printcardshuffle(cards, perm)
end

factbasenums()
</lang>{{output}}<pre>
2.0.0 -> [2, 0, 1, 3]
2.0.1 -> [2, 0, 3, 1]
2.1.0 -> [2, 1, 0, 3]
2.1.1 -> [2, 1, 3, 0]
2.2.0 -> [2, 3, 0, 1]
2.2.1 -> [2, 3, 1, 0]
3.0.0 -> [3, 0, 1, 2]
3.0.1 -> [3, 0, 2, 1]
3.1.0 -> [3, 1, 0, 2]
3.1.1 -> [3, 1, 2, 0]
3.2.0 -> [3, 2, 0, 1]
3.2.1 -> [3, 2, 1, 0]

Permutations generated = 39916800
compared to 11! which = 39916800


Task shuffles:
[39, 50, 7, 49, 30, 32, 2, 14, 12, 4, 36, 46, 42, 22, 17, 43, 40, 51, 24, 35, 3, 8, 41, 9, 1, 26, 37, 15, 45, 34, 5, 6, 10, 18, 0, 21,
48, 38, 20, 19, 44, 47, 33, 13, 29, 28, 31, 25, 11, 16, 23, 27]
A♣3♣7♠4♣T♦8♦Q♠K♥2♠T♠4♦7♣J♣5♥T♥T♣K♣2♣3♥5♦J♠6♠Q♣5♠K♠A♦3♦Q♥8♣6♦9♠8♠4♠9♥A♠6♥5♣2♦7♥8♥9♣6♣7♦A♥J♦Q♦9♦2♥3♠J♥4♥K♦
[51, 48, 16, 23, 3, 0, 22, 39, 34, 2, 44, 37, 15, 42, 17, 40, 43, 35, 20, 30, 11, 19, 4, 7, 21, 9, 14, 36, 13, 49, 25, 31, 41, 45, 33,
47, 24, 8, 46, 38, 29, 18, 26, 28, 32, 10, 27, 1, 50, 12, 6, 5]
2♣5♣J♥4♥J♠A♠5♥A♣6♦Q♠9♣3♦Q♥J♣T♥K♣T♣5♦7♥T♦3♠8♥T♠7♠6♥5♠K♥4♦A♥4♣2♥9♦Q♣8♣7♦6♣3♥6♠7♣2♦J♦9♥A♦Q♦8♦4♠K♦K♠3♣2♠8♠9♠
My random shuffle:
[5, 0, 14, 46, 39, 16, 12, 48, 40, 22, 45, 18, 21, 42, 23, 6, 20, 7, 36, 10, 35, 37, 15, 3, 32, 9, 29, 47, 28, 27, 2, 49, 11, 4, 34, 51,
8, 41, 13, 31, 43, 38, 1, 44, 24, 17, 25, 26, 30, 50, 19, 33]
9♠A♠K♥7♣A♣J♥2♠5♣K♣5♥8♣9♥6♥J♣4♥8♠7♥7♠4♦4♠5♦3♦Q♥J♠8♦5♠J♦6♣Q♦K♦Q♠4♣3♠T♠6♦2♣6♠Q♣A♥9♦T♣2♦K♠9♣3♥T♥2♥A♦T♦3♣8♥7♦
</pre>
</pre>