RosettaCodeData/Task/Permutations/Raku/permutations-5.raku
2023-07-01 13:44:08 -04:00

47 lines
930 B
Raku
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Heaps algorithm for generating permutations. Algorithm 2 in
# Robert Sedgewick, 1977. Permutation generation methods. ACM
# Comput. Surv. 9, 2 (June 1977), 137-164.
define(n, 3)
define(n_minus_1, 2)
implicit none
integer a(1:n)
integer c(1:n)
integer i, k
integer tmp
10000 format ('(', I1, n_minus_1(' ', I1), ')')
# Initialize the data to be permuted.
do i = 1, n {
a(i) = i
}
# What follows is a non-recursive Heaps algorithm as presented by
# Sedgewick. Sedgewick neglects to fully initialize c, so I have
# corrected for that. Also I compute k without branching, by instead
# doing a little arithmetic.
do i = 1, n {
c(i) = 1
}
i = 2
write (*, 10000) a
while (i <= n) {
if (c(i) < i) {
k = mod (i, 2) + ((1 - mod (i, 2)) * c(i))
tmp = a(i)
a(i) = a(k)
a(k) = tmp
c(i) = c(i) + 1
i = 2
write (*, 10000) a
} else {
c(i) = 1
i = i + 1
}
}
end