Last active
January 1, 2016 16:08
-
-
Save cldotdev/8168466 to your computer and use it in GitHub Desktop.
Algorithm by Donald Knuth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| unsigned long long | |
| choose(unsigned long long n, unsigned long long k) { | |
| if (k > n) { | |
| return 0; | |
| } | |
| unsigned long long r = 1; | |
| for (unsigned long long d = 1; d <= k; ++d) { | |
| r *= n--; | |
| r /= d; | |
| } | |
| return r; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Algorithm by Donald Knuth. */ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| void main( void) | |
| { | |
| int i, j=1, k, n, *c, x; | |
| printf( "Enter n,k: "); | |
| scanf( "%d,%d", &n, &k); | |
| c = malloc( (k+3) * sizeof(int)); | |
| for (i=1; i <= k; i++) c[i] = i; | |
| c[k+1] = n+1; | |
| c[k+2] = 0; | |
| j = k; | |
| visit: | |
| for (i=k; i >= 1; i--) printf( "%3d", c[i]); | |
| printf( "\n"); | |
| if (j > 0) {x = j+1; goto incr;} | |
| if (c[1] + 1 < c[2]) | |
| { | |
| c[1] += 1; | |
| goto visit; | |
| } | |
| j = 2; | |
| do_more: | |
| c[j-1] = j-1; | |
| x = c[j] + 1; | |
| if (x == c[j+1]) {j++; goto do_more;} | |
| if (j > k) exit(0); | |
| incr: | |
| c[j] = x; | |
| j--; | |
| goto visit; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment