Polissage de tirages.py
authorJulien Moutinho <julm@autogeree.net>
Tue, 28 Aug 2018 21:11:07 +0000 (23:11 +0200)
committerJulien Moutinho <julm@autogeree.net>
Tue, 28 Aug 2018 21:11:07 +0000 (23:11 +0200)
tirages.py

index c8e6a0c4154eaad0899d146ddac4ceb602995325..25fc4983d8bf5c45808a1694d43bb1ea5af01c60 100644 (file)
@@ -38,8 +38,8 @@ def nCk(n,k):
        if k > n // 2:
                k = n - k # more efficient and safe with smaller numbers
        r = 1
-       for j in range(1, k+1):
-               r = r * (n-j+1) // j
+       for i in range(1, k+1):
+               r = r * (n-i+1) // i
        #print("nCk%s = %i" % (str((n,k)), r))
        return r
 def nCkLO(n,k):
@@ -93,7 +93,8 @@ def sequenceOfRank(n, k, rank):
        sequenceOfRank(n, len(ns), rankOfSequence(n, ns)) == ns
        
        DOC: <http://www.site.uottawa.ca/~lucia/courses/5165-09/GenCombObj.pdf>, p.75-77
-       Improved to work on k-sequence of permutations instead of only full permutations.
+       Computed from left to right to work on k-sequence of permutations
+       instead of only full permutations.
        """
        try:
                rank = int(rank)
@@ -101,7 +102,7 @@ def sequenceOfRank(n, k, rank):
                return None
        p = []
        a = nAk(n,k)
-       for i in range(1,k+1):
+       for i in range(1,k+1): # first pass from left to right
                a //= n-(i-1) # optimized a = nAk(n-i, k-i)
                d = rank // a # greatest multiple of a, lower or equal to r
                rank = rank % a