Permutations/Rank of a permutation: Difference between revisions

→‎{{header|Julia}}: A new entry for Julia
(→‎{{header|Julia}}: A new entry for Julia)
Line 520:
3867119554188057320189830921237689379982446172130048814726708614069209339326961586538911571940960420075292018388619717817588596221122134526830668232334295443338743434633976186486468178683660450737612737202079329785249513121919049677640145391966805396 --> [100, 47, 42, 69, 16, 43, 66, 107, 73, 79, 12, 80, 41, 50, 9, 126, 95, 36, 26, 51, 123, 45, 52, 3, 93, 29, 83, 17, 82, 5, 81, 85, 131, 122, 113, 6, 75, 28, 59, 64, 138, 20, 74, 114, 27, 65, 105, 116, 62, 142, 141, 35, 115, 4, 49, 78, 2, 97, 130, 89, 110, 57, 90, 127, 72, 119, 44, 13, 99, 112, 118, 103, 77, 125, 92, 133, 104, 60, 76, 70, 23, 53, 55, 38, 108, 84, 96, 54, 128, 140, 25, 11, 24, 7, 124, 136, 8, 111, 46, 63, 31, 1, 102, 67, 94, 0, 132, 37, 91, 10, 101, 22, 34, 68, 14, 71, 21, 87, 30, 40, 129, 120, 18, 58, 61, 86, 56, 143, 32, 33, 15, 88, 137, 98, 106, 109, 135, 134, 48, 139, 121, 39, 19, 117] --> 3867119554188057320189830921237689379982446172130048814726708614069209339326961586538911571940960420075292018388619717817588596221122134526830668232334295443338743434633976186486468178683660450737612737202079329785249513121919049677640145391966805396
4939977610738364532346788397924709243352263476618646232552639571823537393872611379891069437022354024663565745556550101222893478863124970842071831822047445469519025638779057663111504542116305321498448757808078281473228131641155000612722388784688217279 --> [128, 23, 97, 115, 131, 15, 21, 61, 90, 116, 32, 80, 59, 137, 7, 63, 43, 55, 11, 83, 27, 138, 114, 40, 122, 4, 132, 125, 54, 25, 95, 111, 72, 84, 17, 13, 31, 10, 16, 52, 126, 129, 91, 18, 47, 53, 34, 119, 57, 41, 110, 134, 108, 58, 127, 82, 66, 70, 33, 9, 98, 142, 100, 121, 30, 105, 36, 120, 48, 2, 28, 37, 5, 46, 44, 71, 107, 45, 19, 141, 86, 76, 109, 143, 118, 3, 130, 89, 73, 42, 56, 94, 35, 67, 136, 12, 74, 123, 24, 64, 93, 26, 14, 112, 88, 29, 77, 60, 85, 6, 0, 96, 103, 62, 50, 124, 8, 135, 22, 79, 68, 139, 78, 101, 20, 117, 51, 133, 81, 102, 39, 99, 113, 38, 104, 69, 106, 75, 92, 87, 49, 1, 140, 65] --> 4939977610738364532346788397924709243352263476618646232552639571823537393872611379891069437022354024663565745556550101222893478863124970842071831822047445469519025638779057663111504542116305321498448757808078281473228131641155000612722388784688217279</pre>
 
=={{header|Julia}}==
Julia has native support for permutations. Depending upon its arguments, <tt>nthperm</tt> will either return the rank of a permutation or permute a vector according to the provided rank. <tt>randperm(n)</tt> returns a random permutation of <tt>n</tt> objects.
 
Note that, because Julia uses 1-based array indexing, permutations consists of lists of [1, ..., <tt>n</tt>] rather than the [0, ..., <tt>n-1</tt>] used for many of the other solutions to this task. Also, Julian partition ranks range from 1 to <tt>n!</tt> rather than from 0 to <tt>n!-1</tt>.
 
Unfortunately, as of the 0.3 release of Julian, this code can not be used to address the StackOverflow question. Although many of Julia's permutation built-in functions support large permutations, <tt>nthperm(p)</tt> is limited to partitions of no more than 20 objects. <tt>p = randperm(114)</tt> works fine, but <tt>nthperm(p)</tt> throws an <tt>OverflowError</tt>. Arguably, this is a bug, and it may be rectified in future releases.
<lang Julia>
nobjs = 4
a = collect(1:nobjs)
println("All permutations of ", nobjs, " objects:")
for i in 1:factorial(nobjs)
p = nthperm(a, i)
prank = nthperm(p)
print(@sprintf("%5d => ", i))
println(p, " (", prank, ")")
end
 
nobjs = 12
nsamp = 4
ptaken = Int[]
println()
println(nsamp, " random permutations of ", nobjs, " objects:")
for i in 1:nsamp
p = randperm(nobjs)
prank = nthperm(p)
while prank in ptaken
p = randperm(nobjs)
prank = nthperm(p)
end
push!(ptaken, prank)
println(" ", p, " (", prank, ")")
end
</lang>
 
{{out}}
<pre>
All permutations of 4 objects:
1 => [1,2,3,4] (1)
2 => [1,2,4,3] (2)
3 => [1,3,2,4] (3)
4 => [1,3,4,2] (4)
5 => [1,4,2,3] (5)
6 => [1,4,3,2] (6)
7 => [2,1,3,4] (7)
8 => [2,1,4,3] (8)
9 => [2,3,1,4] (9)
10 => [2,3,4,1] (10)
11 => [2,4,1,3] (11)
12 => [2,4,3,1] (12)
13 => [3,1,2,4] (13)
14 => [3,1,4,2] (14)
15 => [3,2,1,4] (15)
16 => [3,2,4,1] (16)
17 => [3,4,1,2] (17)
18 => [3,4,2,1] (18)
19 => [4,1,2,3] (19)
20 => [4,1,3,2] (20)
21 => [4,2,1,3] (21)
22 => [4,2,3,1] (22)
23 => [4,3,1,2] (23)
24 => [4,3,2,1] (24)
 
4 random permutations of 12 objects:
[5,9,4,1,11,12,6,8,10,2,7,3] (186192332)
[10,12,4,3,2,9,7,1,8,6,11,5] (396717496)
[8,1,2,5,10,3,11,9,12,7,6,4] (279524016)
[1,10,4,12,6,5,8,9,3,2,7,11] (30095719)
</pre>
 
=={{header|Mathematica}}==