Solve hanging lantern problem: Difference between revisions

→‎Version 2: Made a bit more general and added more examples.
m (→‎full solution: merged the two approaches, much faster)
(→‎Version 2: Made a bit more general and added more examples.)
Line 937:
===Version 2===
{{libheader|Wren-perm}}
{{libheader|Wren-big}}
Alternatively, using library methods.
<lang ecmascript>import "./perm" for Perm
import "./big" for BigInt
 
var listPerms = Fn.new { |a, rowSize|
System.print("Number of permutations for n (<= 5) groups and lanterns per group [1..n]:")
var permlows = List.filled(6a.count, 0)
var currsum = lows.toList0
var mlist = []
for (i in 0..5.a.count) {
perm[i]sum = curr[psum + a[i]-1]
lows[i] = sum
mlist = mlist + [i+1] * a[i]
}
var n = Perm.countDistinct(sum, a)
System.print("\nList of %(n) permutations for %(a.count) groups and lanterns per group %(a):")
var count = 0
for (p in Perm.listDistinct([1, 2, 2, 3, 3, 3]mlist)) {
var curr = lows.toList
var perm = List.filled(sum, 0)
for (i in 0...sum) {
curr[p perm[i]-1] = curr[p[i]-1] - 1
curr[p[i]-1] = curr[p[i]-1] - 1
}
System.printwrite("%(perm) ")
count = count + 1
if (count % rowSize == 0) System.print()
}
if (count % rowSize != 0) System.print()
}
 
System.print("Number of permutations for n (<= 5) groups andthe lanterns per group [1..n]shown:")
var n = 0
for (i in 1..59) {
var a = (1..i).toList
n = n + i
System.print("%(a) => %(PermBigInt.countDistinctmultinomial(n, a))")
}
[6,var 5,a 4,= [1, 3, 23]
 
System.print("\nList%(a) of=> permutations for 3 groups and lanterns per group [1, 2%(BigInt.multinomial(7, 3]:a))")
var lowsa = [110, 314, 612]
System.print("%(a) => %(BigInt.multinomial(36, a))")
for (p in Perm.listDistinct([1, 2, 2, 3, 3, 3])) {
listPerms.call([1, 2, 3], 4)
var curr = lows.toList
listPerms.call([1, 3, 3], 3)</lang>
var perm = List.filled(6, 0)
for (i in 0..5) {
perm[i] = curr[p[i]-1]
curr[p[i]-1] = curr[p[i]-1] - 1
}
System.print(perm)
}</lang>
 
{{out}}
<pre>
Number of permutations for n (<= 5) groups andthe lanterns per group [1..n]shown:
[1] => 1
[1, 2] => 3
Line 968 ⟶ 990:
[1, 2, 3, 4] => 12600
[1, 2, 3, 4, 5] => 37837800
[1, 2, 3, 4, 5, 6] => 2053230379200
[1, 2, 3, 4, 5, 6, 7] => 2431106898187968000
[1, 2, 3, 4, 5, 6, 7, 8] => 73566121315513295589120000
[1, 2, 3, 4, 5, 6, 7, 8, 9] => 65191584694745586153436251091200000
[6, 1, 3, 2,3] 5,=> 4]140
[10, 14, 12] => 2454860399191200
 
List of 60 permutations for 3 groups and lanterns per group [1, 2, 3]:
[1, 3, 2, 6, 5, 4] [1, 3, 6, 2, 5, 4] [1, 3, 6, 5, 2, 4] [1, 3, 6, 5, 4, 2] [1, 6, 3, 2, 5, 4]
[1, 6, 3, 5, 2, 4] [1, 6, 3, 5, 4, 2] [1, 6, 5, 3, 2, 4] [1, 6, 5, 3, 4, 2] [1, 6, 5, 4, 3, 2]
[3, 1, 2, 6, 5, 4] [3, 1, 6, 2, 5, 4] [3, 1, 6, 5, 2, 4] [3, 1, 6, 5, 4, 2] [3, 2, 1, 6, 5, 4]
[3, 2, 6, 1, 5, 4] [3, 2, 6, 5, 1, 4] [3, 2, 6, 5, 4, 1] [3, 6, 2, 1, 5, 4] [3, 6, 2, 5, 1, 4]
[3, 6, 2, 5, 4, 1] [3, 6, 1, 2, 5, 4] [3, 6, 1, 5, 2, 4] [3, 6, 1, 5, 4, 2] [3, 6, 5, 1, 2, 4]
[3, 6, 5, 1, 4, 2] [3, 6, 5, 2, 1, 4] [3, 6, 5, 2, 4, 1] [3, 6, 5, 4, 2, 1] [3, 6, 5, 4, 1, 2]
[6, 3, 2, 1, 5, 4] [6, 3, 2, 5, 1, 4] [6, 3, 2, 5, 4, 1] [6, 3, 1, 2, 5, 4] [6, 3, 1, 5, 2, 4]
[6, 3, 1, 5, 4, 2] [6, 3, 5, 1, 2, 4] [6, 3, 5, 1, 4, 2] [6, 3, 5, 2, 1, 4] [6, 3, 5, 2, 4, 1]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