Skip to content

Commit 493792b

Browse files
Add Modular Exponentiation (TheAlgorithms#263)
1 parent 255ce86 commit 493792b

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Algorithms.Numeric;
2+
using System;
3+
using NUnit.Framework;
4+
using FluentAssertions;
5+
6+
namespace Algorithms.Tests.Numeric
7+
{
8+
public class ModularExponentiationTest
9+
{
10+
[Test]
11+
[TestCase(3, 6, 11, 3)]
12+
[TestCase(5, 3, 13, 8)]
13+
[TestCase(2, 7, 17, 9)]
14+
[TestCase(7, 4, 16, 1)]
15+
[TestCase(7, 2, 11, 5)]
16+
[TestCase(4, 13, 497, 445)]
17+
[TestCase(13, 3, 1, 0)]
18+
public void ModularExponentiationCorrect(int b, int e, int m, int expectedRes)
19+
{
20+
var modularExponentiation = new ModularExponentiation();
21+
var actualRes = modularExponentiation.ModularPow(b, e, m);
22+
actualRes.Should().Be(expectedRes);
23+
}
24+
25+
[TestCase(17, 7, -3)]
26+
[TestCase(11, 3, -5)]
27+
[TestCase(14, 3, 0)]
28+
public void ModularExponentiationNegativeMod(int b, int e, int m)
29+
{
30+
var modularExponentiation = new ModularExponentiation();
31+
Action res = () => modularExponentiation.ModularPow(b, e, m);
32+
res.Should().Throw<ArgumentException>()
33+
.WithMessage(String.Format("{0} is not a positive integer", m));
34+
}
35+
}
36+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
3+
namespace Algorithms.Numeric
4+
{
5+
/// <summary>
6+
/// Modular exponentiation is a type of exponentiation performed over a modulus
7+
/// Modular exponentiation c is: c = b^e mod m where b is base, e is exponent, m is modulus
8+
/// (Wiki: https://en.wikipedia.org/wiki/Modular_exponentiation).
9+
/// </summary>
10+
public class ModularExponentiation
11+
{
12+
/// <summary>
13+
/// Performs Modular Exponentiation on b, e, m.
14+
/// </summary>
15+
/// <param name="b">Base.</param>
16+
/// <param name="e">Exponent.</param>
17+
/// <param name="m">Modulus.</param>
18+
/// <returns>Modular Exponential.</returns>
19+
public int ModularPow(int b, int e, int m)
20+
{
21+
// initialize result in variable res
22+
int res = 1;
23+
if (m == 1)
24+
{
25+
// 1 divides every number
26+
return 0;
27+
}
28+
29+
if (m <= 0)
30+
{
31+
// exponential not defined in this case
32+
throw new ArgumentException(string.Format("{0} is not a positive integer", m));
33+
}
34+
35+
for (int i = 0; i < e; i++)
36+
{
37+
res = (res * b) % m;
38+
}
39+
40+
return res;
41+
}
42+
}
43+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ This repository contains algorithms and data structures implemented in C# for ed
3939
* [Binary GCD](./Algorithms/Numeric/GreatestCommonDivisor/BinaryGreatestCommonDivisorFinder.cs)
4040
* [Factorization](./Algorithms/Numeric/Factorization)
4141
* [Trial division Factorization](./Algorithms/Numeric/Factorization/TrialDivisionFactorizer.cs)
42+
* [Modular Exponentiation](./Algorithms/Numeric/ModularExponentiation.cs)
4243
* [Series](./Algorithms/Numeric/Series)
4344
* [Maclaurin Series](./Algorithms/Numeric/Series/Maclaurin.cs)
4445
* [Gauss-Jordan Elimination](./Algorithms/Numeric/GaussJordanElimination.cs)

0 commit comments

Comments
 (0)