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

(Corrected per dıscussıon page)
Line 415:
Seems to me it would be easier to just say: Ω.pick(*).join
5♦3♠8♦10♦2♥7♠7♦Q♦A♠5♣8♣Q♠4♠2♦K♦5♠Q♥7♣10♠2♠K♠J♣9♣3♣4♥3♥4♦3♦Q♣2♣4♣J♦9♠A♣J♠10♣6♣9♦6♠10♥6♥9♥J♥7♥K♥A♦8♠A♥5♥8♥K♣6♦</pre>
 
=={{header|Phix}}==
<lang Phix>function fperm(sequence fbn, omega)
integer m=0
for i=1 to length(fbn) do
integer g = fbn[i]
if g>0 then
omega[m+1..m+g+1] = omega[m+g+1]&omega[m+1..m+g]
end if
m += 1
end for
return omega
end function
 
function factorial_base_numbers(integer size, bool countOnly)
-- translation of Go
sequence results = {}, res = repeat(0,size)
integer count = 0, n = 0
while true do
integer radix = 2, k = n
while k>0 do
if not countOnly
and radix <= size+1 then
res[size-radix+2] = mod(k,radix)
end if
k = floor(k/radix)
radix += 1
end while
if radix > size+2 then exit end if
count += 1
if not countOnly then
results = append(results, res)
end if
n += 1
end while
return iff(countOnly?count:results)
end function
 
sequence fbns = factorial_base_numbers(3,false)
for i=1 to length(fbns) do
printf(1,"%v -> %v\n",{fbns[i],fperm(fbns[i],{0,1,2,3})})
end for
printf(1,"\n")
 
integer count = factorial_base_numbers(10,true)
printf(1,"Permutations generated = %d\n", count)
printf(1," versus factorial(11) = %d\n", factorial(11))
 
procedure show_cards(sequence s)
printf(1,"\n")
for i=1 to length(s) do
integer c = s[i]-1
string sep = iff(mod(i,13)=0 or i=length(s)?"\n":" ")
puts(1,"AKQJT98765432"[mod(c,13)+1]&"SHDC"[floor(c/13)+1]&sep)
end for
end procedure
 
function rand_fbn51()
sequence fbn51 = repeat(0,51)
for i=1 to 51 do
fbn51[i] = rand(52-i)
end for
return fbn51
end function
 
sequence fbn51s = {{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},
rand_fbn51()}
for i=1 to length(fbn51s) do
show_cards(fperm(fbn51s[i],tagset(52)))
end for</lang>
{{out}}
<pre>
{0,0,0} -> {0,1,2,3}
{0,0,1} -> {0,1,3,2}
{0,1,0} -> {0,2,1,3}
{0,1,1} -> {0,2,3,1}
{0,2,0} -> {0,3,1,2}
{0,2,1} -> {0,3,2,1}
{1,0,0} -> {1,0,2,3}
{1,0,1} -> {1,0,3,2}
{1,1,0} -> {1,2,0,3}
{1,1,1} -> {1,2,3,0}
{1,2,0} -> {1,3,0,2}
{1,2,1} -> {1,3,2,0}
{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
versus factorial(11) = 39916800
 
AC 3C 7S 4C TD 8D QS KH 2S TS 4D 7C JC
5H TH TC KC 2C 3H 5D JS 6S QC 5S KS AD
3D QH 8C 6D 9S 8S 4S 9H AS 6H 5C 2D 7H
8H 9C 6C 7D AH JD QD 9D 2H 3S JH 4H KD
 
2C 5C JH 4H JS AS 5H AC 6D QS 9C 3D QH
JC TH KC TC 5D 7H TD 3S 8H TS 7S 6H 5S
KH 4D AH 4C 2H 9D QC 8C 7D 6C 3H 6S 7C
2D JD 9H AD QD 8D 4S KD KS 3C 2S 8S 9S
 
JS 4H JD 9H 2C 9C 3C KH 9S TH 6D 5S 3H
2H 3S JH 5H QD 4C 7D 4S QC 7C TS 5C 6H
KS 5D QH 2S AD AC 7S QS TC JC 7H 6C 8H
KC 9D 4D 8D KD 6S TD AH 8C 2D 8S 3D AS
</pre>
 
=={{header|zkl}}==
7,795

edits