forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinomialCoefficient.cs
More file actions
27 lines (25 loc) · 841 Bytes
/
Copy pathBinomialCoefficient.cs
File metadata and controls
27 lines (25 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;
namespace Algorithms.Numeric
{
/// <summary>
/// The binomial coefficients are the positive integers
/// that occur as coefficients in the binomial theorem.
/// </summary>
public static class BinomialCoefficient
{
/// <summary>
/// Calculates Binomial coefficients for given input.
/// </summary>
/// <param name="num">First number.</param>
/// <param name="k">Second number.</param>
/// <returns>Binimial Coefficients.</returns>
public static long Calculate(int num, int k)
{
if (num < k || k < 0)
{
throw new ArgumentException("n ≥ k ≥ 0");
}
return Factorial.Calculate(num) / (Factorial.Calculate(k) * Factorial.Calculate(num - k));
}
}
}