Skip to content

Commit f0f8cad

Browse files
authored
Merge pull request #55 from davidavdav/nthperm-speed
nthperm(): Replace one `%=` by a `-= remainder * divisor` computed earlier
2 parents 0129280 + beee7ec commit f0f8cad

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/permutations.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,10 @@ function nthperm!(a::AbstractVector, k::Integer)
152152
0 < k <= f || throw(ArgumentError("permutation k must satisfy 0 < k ≤ $f, got $k"))
153153
k -= 1 # make k 1-indexed
154154
for i=1:n-1
155-
f = div(f, n - i + 1)
156-
j = div(k, f) + 1
157-
k = k % f
158-
159-
j = j+i-1
155+
f ÷= n - i + 1
156+
j = k ÷ f
157+
k -= j * f
158+
j += i
160159
elt = a[j]
161160
for d = j:-1:i+1
162161
a[d] = a[d-1]

0 commit comments

Comments
 (0)