Modular Arithmetic - GeeksforGeeks (2024)

Last Updated : 09 Apr, 2024

Improve

Modular arithmetic is the branch of arithmetic mathematics related with the “mod” functionality. Basically, modular arithmetic is related with computation of “mod” of expressions. Expressions may have digits and computational symbols of addition, subtraction, multiplication, division or any other. Here we will discuss briefly about all modular arithmetic operations.

Quotient Remainder Theorem:

It states that, for any pair of integers a and b (b is positive), there exist two unique integers q and r such that:

a = b x q + r where 0 <= r < b

Example: If a = 20, b = 6 then q = 3, r = 2 20 = 6 x 3 + 2

Modular Addition:

Rule for modular addition is:

(a + b) mod m = ((a mod m) + (b mod m)) mod m

Example:

(15 + 17) % 7
= ((15 % 7) + (17 % 7)) % 7
= (1 + 3) % 7
= 4 % 7
= 4

The same rule is to modular subtraction. We don’t require much modular subtraction but it can also be done in the same way.

Modular Multiplication:

The Rule for modular multiplication is:

(a x b) mod m = ((a mod m) x (b mod m)) mod m

Example:

(12 x 13) % 5
= ((12 % 5) x (13 % 5)) % 5
= (2 x 3) % 5
= 6 % 5
= 1

Modular Division:

The modular division is totally different from modular addition, subtraction and multiplication. It also does not exist always.

(a / b) mod m is not equal to ((a mod m) / (b mod m)) mod m.

This is calculated using the following formula:

(a / b) mod m = (a x (inverse of b if exists)) mod m

Modular Inverse:

The modular inverse of a mod m exists only if a and m are relatively prime i.e. gcd(a, m) = 1. Hence, for finding the inverse of a under modulo m, if (a x b) mod m = 1 then b is the modular inverse of a.

Example: a = 5, m = 7 (5 x 3) % 7 = 1 hence, 3 is modulo inverse of 5 under 7.

Modular Exponentiation:

Finding a^b mod m is the modular exponentiation. There are two approaches for this – recursive and iterative.

Example:

a = 5, b = 2, m = 7
(5 ^ 2) % 7 = 25 % 7 = 4

There is often a need to efficiently calculate the value of xn mod m. This can be done in O(logn) time using the following recursion:

Modular Arithmetic - GeeksforGeeks (1)

It is important that in the case of an even n, the value of xn/2 is calculated only once.

This guarantees that the time complexity of the algorithm is O(logn) because n is always halved when it is even.

The following function calculates the value of xn mod m:

int modpower(int x, int n, int m)
{
if (n == 0)
return 1%m;
long long u = modpower(x,n/2,m);
u = (u*u)%m;
if (n%2 == 1)
u = (u*x)%m;
return u;
}
C++
#include<bits/stdc++.h>#include<iostream>using namespace std;//function that calculate modular exponentiation x^n mod m.int modpower(int x, int n, int m) { if (n == 0) //base case  return 1%m;  long long u = modpower(x,n/2,m);  u = (u*u)%m; if (n%2 == 1) //when 'n' is odd u = (u*x)%m; return u;}//driver functionint main(){  cout<<modpower(5,2,7)<<endl; return 0;}
C
#include <stdio.h>//function that calculate modular exponentiation x^n mod m.int modpower(int x, int n, int m) { if (n == 0) //base case  return 1 % m;  long long u = modpower(x, n / 2, m);  u = (u * u) % m; if (n % 2 == 1) // when 'n' is odd u = (u * x) % m; return u;}//driver functionint main(){  printf("%d\n", modpower(5, 2, 7)); return 0;}
Java
import java.util.*;class GFG { //function that calculate modular exponentiation x^n mod m. public static int modpower(int x, int n, int m) { if (n == 0) //base case  return 1 % m;  long u = modpower(x, n / 2, m);  u = (u * u) % m; if (n % 2 == 1) // when 'n' is odd u = (u * x) % m; return (int)u; } //driver function public static void main(String[] args) { System.out.println(modpower(5, 2, 7)); }}
Python
#function that calculate modular exponentiation x^n mod m.def modpower(x, n, m): if n == 0: # base case return 1 % m u = modpower(x, n // 2, m) u = (u * u) % m if n % 2 == 1: # when 'n' is odd u = (u * x) % m return u#driver functionprint(modpower(5, 2, 7))
Javascript
// function that calculates modular exponentiation x^n mod m.function modpower(x, n, m) { if (n == 0) { // base case return 1 % m; } let u = modpower(x, Math.floor(n / 2), m); u = (u * u) % m; if (n % 2 == 1) { // when 'n' is odd u = (u * x) % m; } return u;}// driver functionconsole.log(modpower(5, 2, 7));
output:
4

Time complexity: O(logn), because n is always halved when it is even.

Fermat’s theorem states that

xm−1 mod m = 1

when m is prime and x and m are coprime. This also yields
xk mod m = xk mod (m−1) mod m.

Below are some more important concepts related to Modular Arithmetic

  • Euler’s Totient Function
  • Compute n! under modulo p
  • Wilson’s Theorem
  • How to compute mod of a big number?
  • Find value of y mod (2 raised to power x)

Recent Articles on Modular Arithmetic.



P

pp_pankaj

Improve

Previous Article

Euclidean algorithms (Basic and Extended)

Next Article

Modular Addition

Please Login to comment...

Modular Arithmetic - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Clemencia Bogisich Ret

Last Updated:

Views: 6169

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Clemencia Bogisich Ret

Birthday: 2001-07-17

Address: Suite 794 53887 Geri Spring, West Cristentown, KY 54855

Phone: +5934435460663

Job: Central Hospitality Director

Hobby: Yoga, Electronics, Rafting, Lockpicking, Inline skating, Puzzles, scrapbook

Introduction: My name is Clemencia Bogisich Ret, I am a super, outstanding, graceful, friendly, vast, comfortable, agreeable person who loves writing and wants to share my knowledge and understanding with you.