Skip to content

Commit

Permalink
[C++] Modular Exponentiation
Browse files Browse the repository at this point in the history
  • Loading branch information
amanmehara committed Mar 16, 2024
1 parent 65bd561 commit b5c38a5
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 53 deletions.
1 change: 1 addition & 0 deletions C++/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* GCD _(Euclidean Algorithm)_
* GCD and Coefficients of Bézout's identity _(Extended Euclidean Algorithm)_
* LCM
* Modular Exponantiation
* Prime Numbers [1 → N] _(Sieve of Eratosthenes)_
* Prime Factorization _(Trial Division)_
* Prime Factorization _(Precomputed Primes)_
Expand Down
2 changes: 1 addition & 1 deletion C++/mathematics/number_theory/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
target_sources(programming
PRIVATE
binary_exponentiation.h
exponentiation.h
factorization.h
gcd.h
lcm.h
Expand Down
51 changes: 0 additions & 51 deletions C++/mathematics/number_theory/binary_exponentiation.h

This file was deleted.

101 changes: 101 additions & 0 deletions C++/mathematics/number_theory/exponentiation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2021 Aman Mehara
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef MEHARA_EXPONENTIATION_H_
#define MEHARA_EXPONENTIATION_H_

#include <concepts>
#include <vector>

namespace mehara::mathematics {

template <typename T>
requires std::integral<T>
T binary_exponentiation_recursive(T a, T b)
{
if (b == 0) {
return 1;
}
auto power = binary_exponentiation_recursive(a, b / 2);
return (b & 1) ? power * power * a : power * power;
}

template <typename T>
requires std::integral<T>
T binary_exponentiation_iterative(T a, T b)
{
T power = 1;
while (b > 0) {
if (b & 1) {
power = power * a;
}
a = a * a;
b >>= 1;
}
return power;
}

template <typename T>
requires std::integral<T>
T binary_exponentiation(T a, T b, bool is_recursive = false)
{
if (is_recursive) {
return binary_exponentiation_recursive(a, b);
}
else {
return binary_exponentiation_iterative(a, b);
}
}

template <typename T>
requires std::integral<T>
T modular_exponentiation_recursive(T a, T b, T m)
{
if (b == 0) {
return 1;
}
auto power = modular_exponentiation_recursive(a, b / 2, m);
return (b & 1) ? (power * power % m) * a % m : power * power % m;
}

template <typename T>
requires std::integral<T>
T modular_exponentiation_iterative(T a, T b, T m)
{
T power = 1;
while (b > 0) {
if (b & 1) {
power = power * a % m;
}
a = a * a % m;
b >>= 1;
}
return power;
}

template <typename T>
requires std::integral<T>
T modular_exponentiation(T a, T b, T m, bool is_recursive = false)
{
if (is_recursive) {
return modular_exponentiation_recursive(a, b, m);
}
else {
return modular_exponentiation_iterative(a, b, m);
}
}

} // namespace mehara::mathematics

#endif // MEHARA_EXPONENTIATION_H_
14 changes: 13 additions & 1 deletion C++/test/mathematics_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "../mathematics/linear_algebra/determinant.h"
#include "../mathematics/linear_algebra/gauss_jordan_elimination.h"
#include "../mathematics/number_theory/binary_exponentiation.h"
#include "../mathematics/number_theory/exponentiation.h"
#include "../mathematics/number_theory/factorization.h"
#include "../mathematics/number_theory/gcd.h"
#include "../mathematics/number_theory/lcm.h"
Expand Down Expand Up @@ -50,6 +50,18 @@ TEST(mathematics, binary_exponentiation)
ASSERT_EQ(243, binary_exponentiation_recursive(3, 5));
ASSERT_EQ(256, binary_exponentiation_iterative(2, 8));
ASSERT_EQ(243, binary_exponentiation_iterative(3, 5));
ASSERT_EQ(256, binary_exponentiation(2, 8));
ASSERT_EQ(243, binary_exponentiation(3, 5));
}

TEST(mathematics, modular_exponentiation)
{
ASSERT_EQ(4, modular_exponentiation_recursive(2, 8, 7));
ASSERT_EQ(1, modular_exponentiation_recursive(3, 5, 11));
ASSERT_EQ(4, modular_exponentiation_iterative(2, 8, 7));
ASSERT_EQ(1, modular_exponentiation_iterative(3, 5, 11));
ASSERT_EQ(4, modular_exponentiation(2, 8, 7));
ASSERT_EQ(1, modular_exponentiation(3, 5, 11));
}

TEST(mathematics, factorization_trial_division)
Expand Down

0 comments on commit b5c38a5

Please sign in to comment.