Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
modular_division.cpp File Reference

An algorithm to divide two numbers under modulo p Modular Division More...

#include <cassert>
#include <iostream>
Include dependency graph for modular_division.cpp:

Namespaces

namespace  math
 for IO operations
 
namespace  modular_division
 Functions for Modular Division implementation.
 

Functions

uint64_t math::modular_division::power (uint64_t a, uint64_t b, uint64_t c)
 This function calculates a raised to exponent b under modulo c using modular exponentiation.
 
uint64_t math::modular_division::mod_division (uint64_t a, uint64_t b, uint64_t p)
 This function calculates modular division.
 
static void test ()
 
int main (int argc, char *argv[])
 Main function.
 

Detailed Description

An algorithm to divide two numbers under modulo p Modular Division

To calculate division of two numbers under modulo p Modulo operator is not distributive under division, therefore we first have to calculate the inverse of divisor using Fermat's little theorem Now, we can multiply the dividend with the inverse of divisor and modulo is distributive over multiplication operation. Let, We have 3 numbers a, b, p To compute (a/b)p (a/b)p ≡ (a*(inverse(b)))p ≡ ((ap)*inverse(b)p)p NOTE: For the existence of inverse of 'b', 'b' and 'p' must be coprime For simplicity we take p as prime Time Complexity: O(log(b)) Example: ( 24 / 3 ) % 5 => 8 % 5 = 3 — (i) Now the inverse of 3 is 2 (24 * 2) % 5 = (24 % 5) * (2 % 5) = (4 * 2) % 5 = 3 — (ii) (i) and (ii) are equal hence the answer is correct.

See also
modular_inverse_fermat_little_theorem.cpp, modular_exponentiation.cpp
Author
Shubham Yadav

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Main function.

Parameters
argccommandline argument count (ignored)
argvcommandline array of arguments (ignored)
Returns
0 on exit
112 {
113 test(); // execute the tests
114 return 0;
115}
static void test()
Definition modular_division.cpp:88
Here is the call graph for this function:

◆ mod_division()

uint64_t math::modular_division::mod_division ( uint64_t a,
uint64_t b,
uint64_t p )

This function calculates modular division.

Parameters
ainteger dividend
binteger divisor
pinteger modulo
Returns
a/b modulo c

Calculate the inverse of b

Calculate the final result

74 {
75 uint64_t inverse = power(b, p - 2, p) % p; /// Calculate the inverse of b
76 uint64_t result =
77 ((a % p) * (inverse % p)) % p; /// Calculate the final result
78 return result;
79}
uint64_t result(uint64_t n)
Definition fibonacci_sum.cpp:76
void power(int x, int n)
Definition power_for_huge_numbers.cpp:56
Here is the call graph for this function:

◆ power()

uint64_t math::modular_division::power ( uint64_t a,
uint64_t b,
uint64_t c )

This function calculates a raised to exponent b under modulo c using modular exponentiation.

Parameters
ainteger base
bunsigned integer exponent
cinteger modulo
Returns
a raised to power b modulo c

Initialize the answer to be returned

Update a if it is more than or equal to c

In case a is divisible by c;

If b is odd, multiply a with answer

b must be even now

b = b/2

49 {
50 uint64_t ans = 1; /// Initialize the answer to be returned
51 a = a % c; /// Update a if it is more than or equal to c
52 if (a == 0) {
53 return 0; /// In case a is divisible by c;
54 }
55 while (b > 0) {
56 /// If b is odd, multiply a with answer
57 if (b & 1) {
58 ans = ((ans % c) * (a % c)) % c;
59 }
60 /// b must be even now
61 b = b >> 1; /// b = b/2
62 a = ((a % c) * (a % c)) % c;
63 }
64 return ans;
65}

◆ test()

static void test ( )
static

Function for testing power function. test cases and assert statement.

Returns
void
88 {
89 uint64_t test_case_1 = math::modular_division::mod_division(8, 2, 2);
90 assert(test_case_1 == 0);
91 std::cout << "Test 1 Passed!" << std::endl;
92 uint64_t test_case_2 = math::modular_division::mod_division(15, 3, 7);
93 assert(test_case_2 == 5);
94 std::cout << "Test 2 Passed!" << std::endl;
95 uint64_t test_case_3 = math::modular_division::mod_division(10, 5, 2);
96 assert(test_case_3 == 0);
97 std::cout << "Test 3 Passed!" << std::endl;
98 uint64_t test_case_4 = math::modular_division::mod_division(81, 3, 5);
99 assert(test_case_4 == 2);
100 std::cout << "Test 4 Passed!" << std::endl;
101 uint64_t test_case_5 = math::modular_division::mod_division(12848, 73, 29);
102 assert(test_case_5 == 2);
103 std::cout << "Test 5 Passed!" << std::endl;
104}
T endl(T... args)
uint64_t mod_division(uint64_t a, uint64_t b, uint64_t p)
This function calculates modular division.
Definition modular_division.cpp:74
Here is the call graph for this function: