Last active
December 31, 2015 10:29
-
-
Save WayneCui/7973858 to your computer and use it in GitHub Desktop.
集合函数:阶乘、排列和组合
This file contains 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
REBOL [] | |
factorial: func [ n [ integer! ]][ | |
factorial-util n 1 | |
] | |
factorial-util: func [ n [ integer! ] init [ integer! ]][ | |
if any [ n = 0 n = 1] [ return init ] | |
return factorial-util n - 1 init * n | |
] | |
permutation: func [ n [ integer! ] m [ integer! ] /local result ][ | |
if m > n [ return "error" ] | |
if m = 1 [ return n ] | |
result: 1 | |
for i 1 m 1 [ | |
result: result * ( n - i + 1 ) | |
] | |
result | |
] | |
permutation2: func [ n [ integer! ] m [ integer! ] ][ | |
permutation2-util n m 1 | |
] | |
permutation2-util: func [ n [ integer! ] m [ integer! ] init [ integer! ]][ | |
if m > n [ return "error"] | |
if m = 0 [ return init ] | |
return permutation2-util n - 1 m - 1 n * init | |
] | |
combination: func [ n [ integer! ] m [ integer! ]][ | |
if m > n [ return 0 ] | |
if m > ( n / 2 + 1) [ return combination n n - m ] | |
;print [permutation n m " / " factorial m] | |
return ( permutation n m ) / (factorial m) | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment