Awk Comb

Combinations of r elements chosen from a pool of n elements

BEGIN {
        for (i=1; i <= r; i++) {
                A[i] = i
                if (i < r ) printf i OFS
                else print i}
        while (A[1] < n - r + 1) {
                for (i = r; i >= 1; i--) {
                        if (A[i] < n - r + i) {
                                A[i]++
                                p = i
                                break}}
                for (i = p + 1; i <= r; i++) A[i] = A[i - 1] + 1
                for (i=1; i <= r; i++) {
                        if (i < r) printf A[i] OFS
                        else print A[i]}}
        exit}

Unknown's avatar

About Linuxgal

Need a spiritual home? Consider joining us at Mary Queen of the Universe Latter-day Buddhislamic Free Will Christian UFO Synagogue of Vishnu
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a comment