Reputation: 1459
In one of the examples in my book it creates two methods. One called combinations and one called factorial. The body on the combinations method contains the following code
private int combinations(int n, int k){
return factorial(n) / (factorial (k) * factorial (n-k));
}
In an example of actually seeing how the math works out for this formula the textbook gives the following example. With n = 5 and k = 2. It gives the following steps and says you should get 10. I'm having difficulty understating the logic.
Does ! have a special meaning in this case? How does 5! = 120 and how does !2 x !3 = 2 x 6?
C (n,k) = n!
_________
k! x (n - k)!
C (5,2) = 5!
___________
2! x !3
= 120
_________
2 x 6
= 10
Upvotes: 1
Views: 4276
Reputation: 3332
! denotes a factorial.
5! = 120
because
5! = 5 * 4 * 3 * 2 * 1
and
2! x 3! = 2 * 6
because
2! x 3! = (2 * 1) * (3 * 2 * 1)
Upvotes: 1
Reputation: 213331
n!
means factorial(n)
. It is equal to: -
n! = n * (n - 1) * (n - 2) * .... * 1
So,
5! = 5 * 4 * 3 * 2 * 1 = 120
And !3
is rather a typo in your book. It doesn't represent a factorial
Upvotes: 1
Reputation: 234847
The !
(factorial) symbol means the product of all integers up to and including the number. So, for example:
5! = 5 * 4 * 3 * 2 * 1 = 120
This should help clarify what the factorial(int)
method is doing.
By the way, if your book actually printed "!3" instead of "3!", it's a typo.
Upvotes: 0