posted 13 years ago
Combination =
n!
r! × (n - r)!
Permutation =
n!
r!
In all cases n ≥ r and if n = r both nCr = 1 and nPr = 1
You can probably count down from n and up from r; if you multiply downwards, you can reduce the maximum size of intermediate values. Don’t try to work out 10 × 9 × 8 × 7 × 6 × 5 × 4 × 3 × 2 × 1 ÷ 7 ÷ 6 ÷ 5 ÷ 4 ÷ 3 ÷ 2 ÷ 1.
Try 10 ÷ 1 × 9 ÷ 2 × 8 ÷ 3 × 7 ÷ 4 × 6 ÷ 5 × 5 ÷ 6 × 4 ÷ 7 × 3 × 2 × 1
You can speed the process, at the cost of more complicated code, if you can work out how to remove common factors and divisors.