Introduction
This article discusses topics that are frequently used to solve programming problems based on math. It includes the following topics:
- Modular arithmetic
- Modular exponentiation
- Greatest Common Divisor (GCD)
- Extended Euclidean algorithm
- Modular multiplicative inverse
1. Modular arithmetic
When one number is divided by another, the modulo operation finds the remainder. It is denoted by the symbol.
Example
Assume that you have two numbers 5 and 2. is 1 because when 5 is divided by 2, the remainder is 1.
Properties
Note: In the last property, is the multiplicative modulo inverse of b and c.
Examples
If , and , then:
Similarly,
Similarly,
If , and , then the answer in some languages is . However, the answer of the operator cannot be negative.
Therefore, to make the answer positive, add c to the formula and compute it as follows:
When are these properties used?
Assume that a = , b = , and c = . You have to find .
When you multiply a with b, the answer is , which does not conform with the standard integer data types. Therefore, to avoid this we used the properties.
2. Modular exponentiation
Exponentiation is a mathematical operation that is expressed as and computed as ( times).
Exponentiation is a mathematical operation that is expressed as and computed as ( times).
Basic method
While calculating , the most basic solution is broken down into . The new problem is , which is similar to the original problem. Therefore, like in original problem, it is further broken down to .
This is a recursive way of determining the answer to . However, sometimes an equation cannot be broken down any further as in the case of . A C++ code for this solution, considering is as follows:
int recursivePower(int x,int n)
{
if(n==0)
return 1;
return x*recursivePower(x,n-1);
}
The recursive method aligns with the explanation, however, the solution can also be written in an iterative format, which is quite ad hoc. A variable 'result', to which is multiplied for number of times, is maintained.
The iterative code is as follows:
int iterativePower(int x,int n)
{
int result=1;
while(n>0)
{
result=result*x;
n--;
}
return result;
}
Time complexity
With respect to time complexity, it is a fairly efficient O(n) solution. However, when it comes to finding , where can be as large as , this solution will not be suitable.
Optimized method
While calculating , the basis of Binary Exponentiation relies on whether is odd or even.
If is even, then can be broken down to . Programmatically, finding is a one-step process. However, the problem is to find .
Notice how the computation steps were reduced from to in just one step? You can continue to divide the power by as long as it is even.
When is odd, try and convert it into an even value. can be written as . This ensures that is even.
- If is even, replace by .
- If is odd, replace by . becomes even and you can apply the relevant formula.
Example
You are required to compute . The steps are as follows:
- The power of is , which is even. Break it down as follows:
- Find . The power of is , which is odd. Convert it into an even power and then apply the following formula:
- is a one-step computation process
The result is .
This is an efficient method and the ten-step process of determining is reduced to a three-step process. At every step, is divided by . Therefore, the time complexity is O(log N).
The code for the process is as follows:
int binaryExponentiation(int x,int n)
{
if(n==0)
return 1;
else if(n%2 == 0) //n is even
return binaryExponentiation(x*x,n/2);
else //n is odd
return x*binaryExponentiation(x*x,(n-1)/2);
}
An iterative version of this method is as follows:int binaryExponentiation(int x,int n)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
However, storing answers that are too large for their respective datatypes is an issue with this method. In some languages the answer will exceed the range of the datatype while in other languages it will timeout due to large number multiplications. In such instances, you must use modulus (%). Instead of finding , you must find % .
For example, run the implementation of the method to find . The solution will timeout, while the solution will run in time but it will produce garbage values.
To fix this you must use the modulo operation i.e. % in those lines where a temporary answer is computed.
int modularExponentiation(int x,int n,int M)
{
if(n==0)
return 1;
else if(n%2 == 0) //n is even
return modularExponentiation((x*x)%M,n/2,M);
else //n is odd
return (x*modularExponentiation((x*x)%M,(n-1)/2,M))%M;
}
Similarly, the iterative binary exponentiation method can be modified as follows:int modularExponentiation(int x,int n,int M)
{
int result=1;
while(n>0)
{
if(power % 2 ==1)
result=(result * x)%M;
x=(x*x)%M;
n=n/2;
}
return result;
}
Recursive solution analysis
- Time complexity: O(log N)
- Memory complexity: O(log N) because a function call consumes memory and log N recursive function calls are made
Iterative solution analysis
- Time complexity: O(log N)
- Memory complexity: O(1)
3. Greatest Common Divisor (GCD)
The GCD of two or more numbers is the largest positive number that divides all the numbers that are considered. For example, the GCD of 6 and 10 is 2 because it is the largest positive number that can divide both 6 and 10.
Naive approach
Traverse all the numbers from min(A, B) to 1 and check whether the current number divides both A and B. If yes, it is the GCD of A and B.
int GCD(int A, int B) {
int m = min(A, B), gcd;
for(int i = m; i > 0; --i)
if(A % i == 0 && B % i == 0) {
gcd = i;
return gcd;
}
}
Time Complexity
The time complexity of this function is O(min(A, B)).
Euclid's algorithm
The idea behind this algorithm is . It will recurse until .
int GCD(int A, int B) {
if(B==0)
return A;
else
return GCD(B, A % B);
}
Example
If a = 16 and B = 10, then the GCD is computed as follows:
- GCD(16, 10) = GCD(10, 16 % 10) = GCD(10, 6)
- GCD(10, 6) = GCD(6, 10 % 6) = GCD(6, 4)
- GCD(6, 4) = GCD(4, 6 % 4) = GCD(4, 2)
- GCD(4, 2) = GCD(2, 4 % 2) = GCD(2, 0)
Since B = 0, will return 2.
Time complexity
The time complexity is O(log(max(A, B))).
The time complexity is O(log(max(A, B))).
4. Extended Euclidean algorithm
This algorithm is an extended form of Euclid’s algorithm. has a special property so that it can always be represented in the form of an equation i.e. .
The coefficients (x and y) of this equation will be used to find the modular multiplicative inverse. The coefficients can be zero, positive or negative in value.
This algorithm takes two inputs as A and B and returns and coefficients of the above equation as output.
Example
If A=30 and B=20, then where 10 is the GCD of 20 and 30.
Key idea
A.x+B.y=GCD(A,B). ---(1)
You know that . Therefore, you can write the equation as follows: B.+ (A % B).=GCD(A,B). ---(2)
You can write where means floor value .B and substitute it in equation 2. Your equation will be as follows: B.+ (A - .B).=GCD(A,B)
When you solve it further, your equation is as follows: B.( - .)+A.=GCD(A,B). ---(3)
Comparing coefficients in equations 1 and 3, you get the following:
- x=
- y= - .
These equations are key in understanding the extended Euclidean algorithm.
In this algorithm, recursive calls are made to . The values that are returned from recursive calls are and , which are used to get x and y.
Implementation
#include < iostream >
int d, x, y;
void extendedEuclid(int A, int B) {
if(B == 0) {
d = A;
x = 1;
y = 0;
}
else {
extendedEuclid(B, A%B);
int temp = x;
x = y;
y = temp - (A/B)*y;
}
}
int main( ) {
extendedEuclid(16, 10);
cout << ”The GCD of 16 and 10 is ” << d << endl;
cout << ”Coefficients x and y are ”<< x << “and “ << y << endl;
return 0;
}
OutputThe GCD of 16 and 10 is 2.
Coefficients x and y are 2 and -3.
Initially, the extended Euclidean algorithm will run as Euclid's algorithm until you determine or until B = 0. It will then assign x = 1 and y = 0.
In the current scenario, since B = 0 and is A, the equation will be changed to .
The values of d, x, and y in the process of the extendedEuclid( ) function are as follows:
Time complexity
The time complexity of the extended Euclidean algorithm is .
When is this algorithm used?
This algorithm is used when A and B are co-prime. In such cases, x becomes the multiplicative modulo inverse of A under modulo B, and y becomes the multiplicative modulo inverse of B under modulo A. This has been explained in detail in the Modular multiplicative inverse section.
5. Modular multiplicative inverse
What is a multiplicative inverse? If , you are required to find B such that it satisfies the equation. The solution is simple. The value of B is or . Here, B is the multiplicative inverse of A.
What is modular multiplicative inverse? If you have two numbers A and M, you are required to find B such it that satisfies the following equation:
Here B is the modular multiplicative inverse of A under modulo M.
Formally, if you have two integers A and M, B is said to be modular multiplicative inverse of A under modulo M if it satisfies the following equation:
. where B is in the range [1,M-1]
This equation is a formal representation of the equation discussed earlier.
Why is B in the range [1,M-1]?
Since we have B%M, the inverse must be in the range [0,M-1]. However, since 0 is invalid, the inverse must be in the range [1,M-1].
Existence of modular multiplicative inverse
An inverse exists only when A and M are coprime i.e. .
For example, if A=5 and M=12, then Here, 5 is the modular multiplicative inverse of 5 under modulo 12.
Though , but since 17 > 12, it isn't considered.
Though , but since 17 > 12, it isn't considered.
Therefore, the answer is 5.
Approach 1 (naive approach)
int modInverse(int A,int M)
{
A=A%M;
for(int B=1;B<M;B++)
if((A*B)%M)==1)
return B;
}
Time Complexity
The algorithm mentioned above runs in O(M).
Approach 2
A and M are coprime i.e. . In the extended Euclidean algorithm, x is the modular multiplicative inverse of A under modulo M. Therefore, the answer is x. You can use the extended Euclidean algorithm to find the multiplicative inverse.
For example, if A=5 and M=12, then . Therefore, the inverse exists.
(which comes from the extended Euclidean algorithm). Therefore, 5 is the inverse of A=5 under modulo 12.
int d,x,y;
int modInverse(int A, int M)
{
extendedEuclid(A,M);
return (x%M+M)%M; //x may be negative
}
Time complexity
O(log(max(A,M)))
Approach 3 (used only when M is prime)
This approach uses Fermat's Little Theorem.
The theorem specifies the following:
By multiplying with both sides,the equation can be rewritten as follows:
The formula for is in the form of exponents. Therefore, modular exponentiation can be used to determine the result.
For example, if A=5 and M=11 then is the inverse of 5 under modulo 11.
int modInverse(int A,int M)
{
return modularExponentiation(A,M-2,M);
}
Time complexity O(log M)
When is modular inverse used?
Modular inverse is used to solve as follows: After you find the inverse, you can solve this equation easily.
Không có nhận xét nào:
Đăng nhận xét