Modulo Operations in Programming With Negative Results
Last Updated :
18 Jan, 2023
In programming, the modulo operation gives the remainder or signed remainder of a division, after one integer is divided by another integer. It is one of the most used operators in programming. This article discusses when and why the modulo operation yields a negative result.
Examples:
- In C, 3 % 2 returns 1. However, -3 % 2 is -1 and 3 % -2 gives 1.
- In Python, -3 % 2 is 1 and 3 % -2 is -1.
Hence, it's evident that the same expression of -3 % 2 gives different results in different programming languages. This result is more related to mathematics rather than programming. The mathematics mentioned here will help in understanding the questions a little more easily.
To understand why this happens, a little knowledge about Euclidean Division and a few points related to Modular Arithmetic is needed.
Euclidean Division: In arithmetic, Euclidean division or division with the remainder is the process of dividing one integer (the dividend) by another (the divisor), in such a way that it produces a quotient and a remainder smaller than the divisor.
Given two integers a and b ( where b ≠ 0 ), there exists unique integers q and r such that:
a = bq + r ( 0 ≤ r < |b| )
where, |b| denotes the absolute value of b.
In the above equation, each of the four integers has its own name i.e.,
- a is called the dividend.
- b is called the divisor.
- q is called the quotient.
- r is called the remainder.
For Example:
- If a = 9 and b = 4, then q = 2 and r = 1, since 9 = 4 × 2 + 1.
- If a = -9 and b = 4, then q = -3 and r = 3, since -9 = 4 × -3 + 3.
- If a = 9 and b = -4, then q = -2 and r = 1, since 9 = -4 × -2 + 1.
- If a = -9 and b = -4, then q = 3 and r = 3, since -9 = -4 × 3 + 3.
Explanations of the above examples:
- The first example is self-explanatory as 9/4 gives quotient 2 and remainder 1.
- The second example (-9/4) gives quotient -3 and not -2. This happens because if we take -2 as quotient then -9 = 4 × -2 + (-1). A remainder of -1 is generated which does not fulfill the conditions of the Euclidean division. It doesn't make -9/4 = -2 wrong it is just not what we need in this case.
Modular Arithmetic: In mathematics, modular arithmetic is a system of arithmetic for integers, where numbers “wrap around” when reaching a certain value, called the modulus.
Congruence: Given any integer N, called a modulus, two integers a and b are said to be congruent modulo N if they produce the same remainder when divided by N i.e.,
a = pN + r and b = qN + r, where 0 ≤ r < |N| is the common remainder.
Congruence modulo N is denoted as a ≡ b (mod N)
where the parentheses means that (mod N) applies to the entire equation, not just to the right-hand side ( here b).
Examples:
- -5 ≡ 2 (mod 7)
- -14 ≡ 19 (mod 11)
Analyzing the first example. -5 = 7 × -1 + 2 and 2 = 7 × 0 + 2. Both -5 and 2 leave the same remainder 2 when divided by 7. Hence, they are congruent to each other.
Similarly, -14 = 11 × -2 + 8 and 19 = 11 × 1 + 8. Both -14 and 19 leave the same remainder 8 when divided by 11. Hence, they are congruent to each other.
Congruence Classes: Suppose a mod N leaves a remainder r when divided by N satisfying the condition 0 ≤ r < |N|. The set of all the integers congruent to a mod N is called the congruence class of the integer a mod N. For Example:
- The congruence class of 3 mod 2 is {…, -5, -3, -1, 1, 3, 5, 7, … }.
- The congruence class of 7 mod 4 is {…, -9, -5, -1, 3, 7, 11, … }.
Select any integer from the congruence class of the integer a mod N as a representative of that class. In mathematics, the least positive residue, the smallest non-negative integer that belongs to that class is chosen as the representative. For Example:
- The representative of congruence class 3 mod 2 is 1.
- The representative of congruence class 7 mod 4 is 3.
The Least positive residue is chosen because it is the remainder produced after the Euclidean division.
Representative is chosen by Programming languages:
- If both a and N are positive, in all the programming languages, a mod N is the remainder of the Euclidean division. However, the difference in the result is observed when either a or N is negative or both are negative. The answer of a mod N then depends on the implementation of a mod N used by the programming language. The remainder produced by all the programming languages does follow a certain criterion i.e., |r| < |N|.
- However, this still leaves a sign ambiguity if the remainder is non-zero, then two possible choices for the remainder occur, one negative (the greatest negative element of the congruence class of a mod N) and the other positive (the least positive element of the congruence class of a mod N). Other elements do not satisfy the criteria of |r| < |N|.
- That is why there are different results when -3 mod 2 performed in C and Python. Both of them are producing the correct results. They are just not outputting the mathematically chosen representative of a mod N.
- In C, -3 mod 2 returns -1, which is a member class of -3 mod 2. However, Python returns 1, which is again a member class of -3 mod 2. Also, both the answer returned to satisfy the given condition of |r| < |N|.
Implementations of % in Programming languages: Many programming languages like C, C++, Java, JavaScript use an implementation similar to the one given below. The remainder is returned according to the equation
r = a - N*trunc(a/N)
Below is the implementation of the above explanation:
C++14
// C++14 program to illustrate modulo
// operations using the above equation
#include <bits/stdc++.h>
using namespace std;
// Function to calculate and
// return the remainder of a % n
int truncMod(int a, int n)
{
// (a / n) implicitly gives
// the truncated result
int q = a / n;
return a - n * q;
}
// Driver Code
int main()
{
int a, b;
// Modulo of two positive numbers
a = 9;
b = 4;
cout << a << " % "
<< b << " = "
<< truncMod(a, b) << endl;
// Modulo of a negative number
// by a positive number
a = -9;
b = 4;
cout << a << " % "
<< b << " = "
<< truncMod(a, b) << endl;
// Modulo of a positive number
// by a negative number
a = 9;
b = -4;
cout << a << " % "
<< b << " = "
<< truncMod(a, b) << endl;
// Modulo of two negative numbers
a = -9;
b = -4;
cout << a << " % "
<< b << " = "
<< truncMod(a, b) << endl;
}
// This code is contributed by mohit kumar 29
Java
// Java program to illustrate modulo
// operations using the above equation
class GFG {
// Function to calculate and
// return the remainder of a % n
static int truncMod(int a, int n)
{
// (a / n) implicitly gives
// the truncated result
int q = a / n;
return a - n * q;
}
// Driver Code
public static void main(String[] args)
{
int a, b;
// Modulo of two positive numbers
a = 9;
b = 4;
System.out.println(a + " % " + b + " = "
+ truncMod(a, b));
// Modulo of a negative number
// by a positive number
a = -9;
b = 4;
System.out.println(a + " % " + b + " = "
+ truncMod(a, b));
// Modulo of a positive number
// by a negative number
a = 9;
b = -4;
System.out.println(a + " % " + b + " = "
+ truncMod(a, b));
// Modulo of two negative numbers
a = -9;
b = -4;
System.out.println(a + " % " + b + " = "
+ truncMod(a, b));
}
}
Python3
# Python3 program to illustrate modulo
# operations using the above equation
# Function to calculate and
# return the remainder of a % n
def truncMod(a, n):
# (a / n) implicitly gives
# the truncated result
q = a // n
return a - n * q
# Driver Code
if __name__ == '__main__':
# Modulo of two positive numbers
a = 9
b = 4
print(a,"%",b,"=",truncMod(a, b))
# Modulo of a negative number
# by a positive number
a = -9
b = 4
print(a,"%",b,"=",truncMod(a, b))
# Modulo of a positive number
# by a negative number
a = 9
b = -4
print(a,"%",b,"=",truncMod(a, b))
# Modulo of two negative numbers
a = -9
b = -4
print(a,"%",b,"=",truncMod(a, b))
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to calculate and
// return the remainder of a % n
static int truncMod(int a, int n)
{
// (a / n) implicitly gives
// the truncated result
int q = a / n;
return a - n * q;
}
// Driver Code
static public void Main()
{
int a, b;
// Modulo of two positive numbers
a = 9;
b = 4;
Console.WriteLine(a + " % " + b + " = "
+ truncMod(a, b));
// Modulo of a negative number
// by a positive number
a = -9;
b = 4;
Console.WriteLine(a + " % " + b + " = "
+ truncMod(a, b));
// Modulo of a positive number
// by a negative number
a = 9;
b = -4;
Console.WriteLine(a + " % " + b + " = "
+ truncMod(a, b));
// Modulo of two negative numbers
a = -9;
b = -4;
Console.WriteLine(a + " % " + b + " = "
+ truncMod(a, b));
}
}
// This code is contributed by snjoy_62.
JavaScript
<script>
// JavaScript program to illustrate modulo
// operations using the above equation
// Function to calculate and
// return the remainder of a % n
function truncMod(a,n)
{
// (a / n) implicitly gives
// the truncated result
let q = Math.round(a / n);
return a - (n * q);
}
let a, b;
// Modulo of two positive numbers
a = 9;
b = 4;
document.write(a + " % " + b + " = "
+ truncMod(a, b)+"<br>");
// Modulo of a negative number
// by a positive number
a = -9;
b = 4;
document.write(a + " % " + b + " = "
+ truncMod(a, b)+"<br>");
// Modulo of a positive number
// by a negative number
a = 9;
b = -4;
document.write(a + " % " + b + " = "
+ truncMod(a, b)+"<br>");
// Modulo of two negative numbers
a = -9;
b = -4;
document.write(a + " % " + b + " = "
+ truncMod(a, b)+"<br>");
// This code is contributed by patel2127
</script>
Output: 9 % 4 = 1
-9 % 4 = -1
9 % -4 = 1
-9 % -4 = -1
Note: Other programming languages with above implementation generates similar outcome. Notice something interesting, when the dividend is negative, the answer produced will also be negative, and it will be the greatest negative member of that congruence class. When the dividend is positive, the answer produced will also be positive and the least positive member of that congruence class.
Many programming languages like Python, Ruby, and Perl use an implementation similar to the one given below. The remainder is returned according to the equation
r = a - N × floor(a/b)
Below is the implementation of the above explanation:
C++
// C++ program to illustrate modulo
// operations using the above equation
#include <bits/stdc++.h>
using namespace std;
// Function to calculate and
// return the remainder of a % n
int floorMod(int a, int n)
{
// Type casting is necessary
// as (int) / (int) will give
// int result, i.e. -3 / 2
// will give -1 and not -1.5
int q = (int)floor((double)a / n);
// Return the resultant remainder
return a - n * q;
}
// Driver Code
int main()
{
int a, b;
// Modulo of two positive numbers
a = 9;
b = 4;
cout << a << " % " << b
<< " = " << floorMod(a, b) << "\n";
// Modulo of a negative number
// by a positive number
a = -9;
b = 4;
cout << a << " % " << b
<< " = " << floorMod(a, b) << "\n";
// Modulo of a positive number
// by a negative number
a = 9;
b = -4;
cout << a << " % " << b
<< " = " << floorMod(a, b) << "\n";
// Modulo of two negative numbers
a = -9;
b = -4;
cout << a << " % " << b
<< " = " << floorMod(a, b) << "\n";
return 0;
}
// This code is contributed by Kingash
Java
// Java program to illustrate modulo
// operations using the above equation
public class GFG {
// Function to calculate and
// return the remainder of a % n
static int floorMod(int a, int n)
{
// Type casting is necessary
// as (int) / (int) will give
// int result, i.e. -3 / 2
// will give -1 and not -1.5
int q = (int)Math.floor((double)a / n);
// Return the resultant remainder
return a - n * q;
}
// Driver Code
public static void main(String[] args)
{
int a, b;
// Modulo of two positive numbers
a = 9;
b = 4;
System.out.println(a + " % " + b + " = "
+ floorMod(a, b));
// Modulo of a negative number
// by a positive number
a = -9;
b = 4;
System.out.println(a + " % " + b + " = "
+ floorMod(a, b));
// Modulo of a positive number
// by a negative number
a = 9;
b = -4;
System.out.println(a + " % " + b + " = "
+ floorMod(a, b));
// Modulo of two negative numbers
a = -9;
b = -4;
System.out.println(a + " % " + b + " = "
+ floorMod(a, b));
}
}
Python3
# Python program to illustrate modulo
# operations using the above equation
# Function to calculate and return the remainder of a % n
def floor_mod(a,n):
# Use the built-in modulo operator
return a % n
# Driver Code
a = 9
b = 4
print(str(a) + " % " + str(b) + " = " + str(floor_mod(a, b)))
# Modulo of a negative number by a positive number
a = -9
b = 4
print(str(a) + " % " + str(b) + " = " + str(floor_mod(a, b)))
# Modulo of a positive number by a negative number
a = 9
b = -4
print(str(a) + " % " + str(b) + " = " + str(floor_mod(a, b)))
# Modulo of two negative numbers
a = -9
b = -4
print(str(a) + " % " + str(b) + " = " + str(floor_mod(a, b)))
C#
// C# program to illustrate modulo
// operations using the above equation
using System;
class GFG{
// Function to calculate and
// return the remainder of a % n
static int floorMod(int a, int n)
{
// Type casting is necessary
// as (int) / (int) will give
// int result, i.e. -3 / 2
// will give -1 and not -1.5
int q = (int)Math.Floor((double)a / n);
// Return the resultant remainder
return a - n * q;
}
// Driver code
static public void Main()
{
int a, b;
// Modulo of two positive numbers
a = 9;
b = 4;
Console.WriteLine(a + " % " + b + " = " +
floorMod(a, b));
// Modulo of a negative number
// by a positive number
a = -9;
b = 4;
Console.WriteLine(a + " % " + b + " = " +
floorMod(a, b));
// Modulo of a positive number
// by a negative number
a = 9;
b = -4;
Console.WriteLine(a + " % " + b + " = " +
floorMod(a, b));
// Modulo of two negative numbers
a = -9;
b = -4;
Console.WriteLine(a + " % " + b + " = " +
floorMod(a, b));
}
}
// This code is contributed by offbeat
JavaScript
<script>
// JavaScript program to illustrate modulo
// operations using the above equation
// Function to calculate and
// return the remainder of a % n
function floorMod(a,n)
{
// Type casting is necessary
// as (int) / (int) will give
// int result, i.e. -3 / 2
// will give -1 and not -1.5
let q = Math.floor(Math.floor(a / n));
// Return the resultant remainder
return a - n * q;
}
// Driver Code
let a, b;
// Modulo of two positive numbers
a = 9;
b = 4;
document.write(a + " % " + b + " = "
+ floorMod(a, b)+"<br>");
// Modulo of a negative number
// by a positive number
a = -9;
b = 4;
document.write(a + " % " + b + " = "
+ floorMod(a, b)+"<br>");
// Modulo of a positive number
// by a negative number
a = 9;
b = -4;
document.write(a + " % " + b + " = "
+ floorMod(a, b)+"<br>");
// Modulo of two negative numbers
a = -9;
b = -4;
document.write(a + " % " + b + " = "
+ floorMod(a, b)+"<br>");
// This code is contributed by unknown2108
</script>
Output: 9 % 4 = 1
-9 % 4 = 3
9 % -4 = -3
-9 % -4 = -1
Note: Other programming languages having above implementation generates similar outcome. Now, when the divisor is negative, the answer produced will also be negative as well as the greatest negative member of that congruence class. When the divisor is positive, the answer produced will also be positive, and it will be the least positive member of that congruence class.
All languages are producing correct results. They are just choosing different representatives of that solution congruent class. In order to produce only the least positive residue of the congruent class as the answer irrespective of the implementation used.
Below is the implementation of the above approach:
C++
// C++ program for the above idea
#include <bits/stdc++.h>
using namespace std;
// Driver Code
int main()
{
int a, b, r;
// Modulo of two positive numbers
a = 9;
b = 4;
r = a % b > 0 ? a % b : abs(b) + a % b;
cout << a << " % " << b << " = " << r << "\n";
// Modulo of a negative number
// by a positive number
a = -9;
b = 4;
r = a % b > 0 ? a % b : abs(b) + a % b;
cout << a << " % " << b << " = " << r << "\n";
// Modulo of a positive number
// by a negative number
a = 9;
b = -4;
r = a % b > 0 ? a % b : abs(b) + a % b;
cout << a << " % " << b << " = " << r << "\n";
// Modulo of two negative numbers
a = -9;
b = -4;
r = a % b > 0 ? a % b : abs(b) + a % b;
cout << a << " % " << b << " = " << r << "\n";
return 0;
}
// This code is contributed by Kingash
Java
// Java program for the above idea
public class PositiveModulus {
// Driver Code
public static void main(String args[])
{
int a, b, r;
// Modulo of two positive numbers
a = 9;
b = 4;
r = a % b > 0 ? a % b : Math.abs(b) + a % b;
System.out.println(a + " % " + b + " = " + r);
// Modulo of a negative number
// by a positive number
a = -9;
b = 4;
r = a % b > 0 ? a % b : Math.abs(b) + a % b;
System.out.println(a + " % "
+ b + " = " + r);
// Modulo of a positive number
// by a negative number
a = 9;
b = -4;
r = a % b > 0 ? a % b : Math.abs(b) + a % b;
System.out.println(a + " % "
+ b + " = " + r);
// Modulo of two negative numbers
a = -9;
b = -4;
r = a % b > 0 ? a % b : Math.abs(b) + a % b;
System.out.println(a + " % "
+ b + " = " + r);
}
}
Python3
# Modulo of two positive numbers
a = 9
b = 4
# If the modulo is greater than 0, set r to the modulo
# Otherwise, set r to the absolute value of b plus the modulo
r = a % b if a % b > 0 else abs(b) + a % b
print(f"{a} % {b} = {r}")
# Modulo of a negative number by a positive number
a = -9
b = 4
# If the modulo is greater than 0, set r to the modulo
# Otherwise, set r to the absolute value of b plus the modulo
r = a % b if a % b > 0 else abs(b) + a % b
print(f"{a} % {b} = {r}")
# Modulo of a positive number by a negative number
a = 9
b = -4
# If the modulo is greater than 0, set r to the modulo
# Otherwise, set r to the absolute value of b plus the modulo
r = a % b if a % b > 0 else abs(b) + a % b
print(f"{a} % {b} = {r}")
# Modulo of two negative numbers
a = -9
b = -4
# If the modulo is greater than 0, set r to the modulo
# Otherwise, set r to the absolute value of b plus the modulo
r = a % b if a % b > 0 else abs(b) + a % b
print(f"{a} % {b} = {r}")
# This code is contributed by phasing17.
C#
// C# program for the above idea
using System;
using System.Collections.Generic;
class GFG{
// Driver Code
public static void Main(string[] args)
{
int a, b, r;
// Modulo of two positive numbers
a = 9;
b = 4;
r = a % b > 0 ? a % b : Math.Abs(b) + a % b;
Console.WriteLine(a + " % " + b + " = " + r);
// Modulo of a negative number
// by a positive number
a = -9;
b = 4;
r = a % b > 0 ? a % b : Math.Abs(b) + a % b;
Console.WriteLine(a + " % " + b + " = " + r);
// Modulo of a positive number
// by a negative number
a = 9;
b = -4;
r = a % b > 0 ? a % b : Math.Abs(b) + a % b;
Console.WriteLine(a + " % " + b + " = " + r);
// Modulo of two negative numbers
a = -9;
b = -4;
r = a % b > 0 ? a % b : Math.Abs(b) + a % b;
Console.WriteLine(a + " % " + b + " = " + r);
}
}
// This code is contributed by avijitmondal1998
JavaScript
<script>
let a, b, r;
// Modulo of two positive numbers
a = 9;
b = 4;
r = a % b > 0 ? a % b : Math.abs(b) + a % b;
document.write(a + " % " + b + " = " + r+"<br>");
// Modulo of a negative number
// by a positive number
a = -9;
b = 4;
r = a % b > 0 ? a % b : Math.abs(b) + a % b;
document.write(a + " % "
+ b + " = " + r+"<br>");
// Modulo of a positive number
// by a negative number
a = 9;
b = -4;
r = a % b > 0 ? a % b : Math.abs(b) + a % b;
document.write(a + " % "
+ b + " = " + r+"<br>");
// Modulo of two negative numbers
a = -9;
b = -4;
r = a % b > 0 ? a % b : Math.abs(b) + a % b;
document.write(a + " % "
+ b + " = " + r+"<br>");
// This code is contributed by unknown2108
</script>
Output: 9 % 4 = 1
-9 % 4 = 3
9 % -4 = 1
-9 % -4 = 3
Similar Reads
Modular Arithmetic Modular arithmetic is a system of arithmetic for numbers where numbers "wrap around" after reaching a certain value, called the modulus. It mainly uses remainders to get the value after wrap around. It is often referred to as "clock arithmetic. As you can see, the time values wrap after reaching 12
10 min read
Modular Exponentiation (Power in Modular Arithmetic) Given three integers x, n, and M, compute (x^n) % M (remainder when x raised to the power n is divided by M).Examples : Input: x = 3, n = 2, M = 4Output: 1Explanation: 32 % 4 = 9 % 4 = 1.Input: x = 2, n = 6, M = 10Output: 4Explanation: 26 % 10 = 64 % 10 = 4.Table of Content[Naive Approach] Repeated
6 min read
Modular multiplicative inverse Given two integers A and M, find the modular multiplicative inverse of A under modulo M.The modular multiplicative inverse is an integer X such that:A X â¡ 1 (mod M) Note: The value of X should be in the range {1, 2, ... M-1}, i.e., in the range of integer modulo M. ( Note that X cannot be 0 as A*0 m
15+ min read
Multiplicative order In number theory, given an integer A and a positive integer N with gcd( A , N) = 1, the multiplicative order of a modulo N is the smallest positive integer k with A^k( mod N ) = 1. ( 0 < K < N ) Examples : Input : A = 4 , N = 7 Output : 3 explanation : GCD(4, 7) = 1 A^k( mod N ) = 1 ( smallest
7 min read
Modular Multiplication Given three integers a, b, and M, where M is the modulus. Compute the result of the modular multiplication of a and b under modulo M.((aÃb) mod M)Examples:Input: a = 5, b = 3, M = 11Output: 4Explanation: a à b = 5 à 3 = 15, 15 % 11 = 4, so the result is 4.Input: a = 12, b = 15, M = 7Output: 5Explana
6 min read
Modular Division Given three positive integers a, b, and M, the objective is to find (a/b) % M i.e., find the value of (a à b-1 ) % M, where b-1 is the modular inverse of b modulo M.Examples: Input: a = 10, b = 2, M = 13Output: 5Explanation: The modular inverse of 2 modulo 13 is 7, so (10 / 2) % 13 = (10 à 7) % 13 =
13 min read
Modulus on Negative Numbers The modulus operator, denoted as %, returns the remainder when one number (the dividend) is divided by another number (the divisor).Modulus of Positive NumbersProblem: What is 7 mod 5?Solution: From Quotient Remainder Theorem, Dividend=Divisor*Quotient + Remainder7 = 5*1 + 2, which gives 2 as the re
7 min read
Problems on Modular Arithmetic
Euler's criterion (Check if square root under modulo p exists)Given a number 'n' and a prime p, find if square root of n under modulo p exists or not. A number x is square root of n under modulo p if (x*x)%p = n%p. Examples : Input: n = 2, p = 5 Output: false There doesn't exist a number x such that (x*x)%5 is 2 Input: n = 2, p = 7 Output: true There exists a
11 min read
Find sum of modulo K of first N natural numberGiven two integer N ans K, the task is to find sum of modulo K of first N natural numbers i.e 1%K + 2%K + ..... + N%K. Examples : Input : N = 10 and K = 2. Output : 5 Sum = 1%2 + 2%2 + 3%2 + 4%2 + 5%2 + 6%2 + 7%2 + 8%2 + 9%2 + 10%2 = 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0 = 5.Recommended PracticeReve
9 min read
How to compute mod of a big number?Given a big number 'num' represented as string and an integer x, find value of "num % a" or "num mod a". Output is expected as an integer. Examples : Input: num = "12316767678678", a = 10 Output: num (mod a) ? 8 The idea is to process all digits one by one and use the property that xy (mod a) ? ((x
4 min read
Exponential Squaring (Fast Modulo Multiplication)Given two numbers base and exp, we need to compute baseexp under Modulo 10^9+7 Examples: Input : base = 2, exp = 2Output : 4Input : base = 5, exp = 100000Output : 754573817In competitions, for calculating large powers of a number we are given a modulus value(a large prime number) because as the valu
12 min read
Trick for modular division ( (x1 * x2 .... xn) / b ) mod (m)Given integers x1, x2, x3......xn, b, and m, we are supposed to find the result of ((x1*x2....xn)/b)mod(m). Example 1: Suppose that we are required to find (55C5)%(1000000007) i.e ((55*54*53*52*51)/120)%1000000007 Naive Method : Simply calculate the product (55*54*53*52*51)= say x,Divide x by 120 a
9 min read
Modular MultiplicationGiven three integers a, b, and M, where M is the modulus. Compute the result of the modular multiplication of a and b under modulo M.((aÃb) mod M)Examples:Input: a = 5, b = 3, M = 11Output: 4Explanation: a à b = 5 à 3 = 15, 15 % 11 = 4, so the result is 4.Input: a = 12, b = 15, M = 7Output: 5Explana
6 min read
Difference between Modulo and ModulusIn the world of Programming and Mathematics we often encounter the two terms "Modulo" and "Modulus". In programming we use the operator "%" to perform modulo of two numbers. It basically finds the remainder when a number x is divided by another number N. It is denoted by : x mod N where x : Dividend
2 min read
Modulo Operations in Programming With Negative ResultsIn programming, the modulo operation gives the remainder or signed remainder of a division, after one integer is divided by another integer. It is one of the most used operators in programming. This article discusses when and why the modulo operation yields a negative result. Examples: In C, 3 % 2 r
15+ min read
Modulo 10^9+7 (1000000007)In most programming competitions, we are required to answer the result in 10^9+7 modulo. The reason behind this is, if problem constraints are large integers, only efficient algorithms can solve them in an allowed limited time.What is modulo operation: The remainder obtained after the division opera
10 min read
Fibonacci modulo pThe Fibonacci sequence is defined as F_i = F_{i-1} + F_{i-2} where F_1 = 1 and F_2 = 1 are the seeds. For a given prime number p, consider a new sequence which is (Fibonacci sequence) mod p. For example for p = 5, the new sequence would be 1, 1, 2, 3, 0, 3, 3, 1, 4, 0, 4, 4 ⦠The minimal zero of the
5 min read
Modulo of a large Binary StringGiven a large binary string str and an integer K, the task is to find the value of str % K.Examples: Input: str = "1101", K = 45 Output: 13 decimal(1101) % 45 = 13 % 45 = 13 Input: str = "11010101", K = 112 Output: 101 decimal(11010101) % 112 = 213 % 112 = 101 Approach: It is known that (str % K) wh
5 min read
Modular multiplicative inverse from 1 to nGive a positive integer n, find modular multiplicative inverse of all integer from 1 to n with respect to a big prime number, say, 'prime'. The modular multiplicative inverse of a is an integer 'x' such that. a x ? 1 (mod prime) Examples: Input : n = 10, prime = 17 Output : 1 9 6 13 7 3 5 15 2 12 Ex
9 min read
Modular exponentiation (Recursive)Given three numbers a, b and c, we need to find (ab) % cNow why do â% câ after exponentiation, because ab will be really large even for relatively small values of a, b and that is a problem because the data type of the language that we try to code the problem, will most probably not let us store suc
6 min read
Chinese Remainder Theorem
Introduction to Chinese Remainder TheoremWe are given two arrays num[0..k-1] and rem[0..k-1]. In num[0..k-1], every pair is coprime (gcd for every pair is 1). We need to find minimum positive number x such that: x % num[0] = rem[0], x % num[1] = rem[1], .......................x % num[k-1] = rem[k-1] Basically, we are given k numbers which
7 min read
Implementation of Chinese Remainder theorem (Inverse Modulo based implementation)We are given two arrays num[0..k-1] and rem[0..k-1]. In num[0..k-1], every pair is coprime (gcd for every pair is 1). We need to find minimum positive number x such that: x % num[0] = rem[0], x % num[1] = rem[1], ....................... x % num[k-1] = rem[k-1] Example: Input: num[] = {3, 4, 5}, rem[
11 min read
Cyclic Redundancy Check and Modulo-2 DivisionCyclic Redundancy Check or CRC is a method of detecting accidental changes/errors in the communication channel. CRC uses Generator Polynomial which is available on both sender and receiver side. An example generator polynomial is of the form like x3 + x + 1. This generator polynomial represents key
15+ min read
Using Chinese Remainder Theorem to Combine Modular equationsGiven N modular equations: A ? x1mod(m1) . . A ? xnmod(mn) Find x in the equation A ? xmod(m1*m2*m3..*mn) where mi is prime, or a power of a prime, and i takes values from 1 to n. The input is given as two arrays, the first being an array containing values of each xi, and the second array containing
12 min read