Skip to content

Commit 90a4267

Browse files
1fisedisiriak
andcommitted
Factorial and combinations. (#103)
* new numeric algorithms with new tests. * mod on binomial * included suggestions. * Update Algorithms/Numeric/Factorial.cs Co-Authored-By: Andrii Siriak <siryaka@gmail.com>
1 parent ee1b594 commit 90a4267

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using Algorithms.Numeric;
3+
using NUnit.Framework;
4+
5+
namespace Algorithms.Tests.Numeric
6+
{
7+
public static class BinomialCoefficientTests
8+
{
9+
[TestCase(4, 2, 6)]
10+
[TestCase(7, 3, 35)]
11+
public static void CalculateFromPairs(int n, int k, long expected)
12+
{
13+
// Arrange
14+
15+
// Act
16+
var result = BinomialCoefficient.Calculate(n, k);
17+
18+
// Assert
19+
Assert.AreEqual(expected, result);
20+
}
21+
22+
[Test]
23+
[TestCase(3, 7)]
24+
public static void TeoremCalculateThrowsException(int n, int k)
25+
{
26+
// Arrange
27+
28+
// Act
29+
30+
// Assert
31+
_ = Assert.Throws<ArgumentException>(() => BinomialCoefficient.Calculate(n, k));
32+
}
33+
}
34+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using Algorithms.Numeric;
3+
using NUnit.Framework;
4+
5+
namespace Algorithms.Tests.Numeric
6+
{
7+
public static class FactorialTests
8+
{
9+
[Test]
10+
[TestCase(5, 120)]
11+
[TestCase(1, 1)]
12+
[TestCase(0, 1)]
13+
[TestCase(4, 24)]
14+
[TestCase(18, 6402373705728000)]
15+
[TestCase(10, 3628800)]
16+
public static void GetsFactorial(int input, long expected)
17+
{
18+
// Arrange
19+
20+
// Act
21+
var result = Factorial.Calculate(input);
22+
23+
// Assert
24+
Assert.AreEqual(expected, result);
25+
}
26+
27+
[Test]
28+
public static void GetsFactorialExceptionForNonPositiveNumbers([Random(-1000, -1, 10, Distinct = true)]int input)
29+
{
30+
// Arrange
31+
32+
// Act
33+
void Act() => Factorial.Calculate(input);
34+
35+
// Assert
36+
37+
_ = Assert.Throws<ArgumentException>(Act);
38+
}
39+
}
40+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
3+
namespace Algorithms.Numeric
4+
{
5+
/// <summary>
6+
/// The binomial coefficients are the positive integers
7+
/// that occur as coefficients in the binomial theorem.
8+
/// </summary>
9+
public static class BinomialCoefficient
10+
{
11+
/// <summary>
12+
/// Calculates Binomial coefficients for given input.
13+
/// </summary>
14+
/// <param name="num">First number.</param>
15+
/// <param name="k">Second number.</param>
16+
/// <returns>Binimial Coefficients.</returns>
17+
public static long Calculate(int num, int k)
18+
{
19+
if (num < k || k < 0)
20+
{
21+
throw new ArgumentException("n ≥ k ≥ 0");
22+
}
23+
24+
return Factorial.Calculate(num) / (Factorial.Calculate(k) * Factorial.Calculate(num - k));
25+
}
26+
}
27+
}

Algorithms/Numeric/Factorial.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
3+
namespace Algorithms.Numeric
4+
{
5+
/// <summary>
6+
/// The factorial of a positive integer n, denoted by n!,
7+
/// is the product of all positive integers less than or equal to n.
8+
/// </summary>
9+
public static class Factorial
10+
{
11+
/// <summary>
12+
/// Calculates factorial of a number.
13+
/// </summary>
14+
/// <param name="num">Input number.</param>
15+
/// <returns>Factorial of input number.</returns>
16+
public static long Calculate(int num)
17+
{
18+
if (num < 0)
19+
{
20+
throw new ArgumentException("Only for num >= 0");
21+
}
22+
23+
return num == 0 ? 1 : num * Calculate(num - 1);
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)