In simple terms, Modular Arithmetic calculates the remainder of anything divided by anything. The later is called modulus. e.g. ( 15 / 7 ) Quotient: 2, Remainder 1.
In greater sense, modular arithmetic is a system of arithmetic for integers, where numbers "wrap around" upon reaching a certain value—the modulus ( src: Wikipedia ).
Take a look at hour clock. After 12:59 pm, we say 1:00 pm, because we modulo the hours by 12. 13 pm % 12 is 1 pm. (%) represents modulus operator.
Examples: 5 % 2 = 1, 14458948 % 25 = 23
Examples: 5 % 2 = 1, 14458948 % 25 = 23
a≡b (mod n)
This says that a is congruent to b modulo n. It means both a and b has same remainder when divided by n. e.g. 38≡14 (mod 12)
Commonly Used Properties:
- Reflexivity: a ≡ a (mod n)
- Symmetry: a ≡ b (mod n) if and only if b ≡ a (mod n)
- Transitivity: If a ≡ b (mod n) and b ≡ c (mod n), then a ≡ c (mod n)
- If a1 ≡ b1 (mod n) and a2 ≡ b2 (mod n), or if a ≡ b (mod n), then:a + k ≡ b + k (mod n) for any integer k (compatibility with translation)
- k a ≡ k b (mod n) for any integer k (compatibility with scaling)
- a1 + a2 ≡ b1 + b2 (mod n) (compatibility with addition)
- a1 – a2 ≡ b1 – b2 (mod n) (compatibility with subtraction)
- a1 a2 ≡ b1 b2 (mod n) (compatibility with multiplication)
- ak ≡ bk (mod n) for any non-negative integer k (compatibility with exponentiation)
Simple C program to perform Modulo:
In CS, modular arithmetic is used in many algorithms where integers become too big to handle.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main() { | |
int a = 5 % 2; | |
printf("%d\n",a); | |
return 0; | |
} |
In CS, modular arithmetic is used in many algorithms where integers become too big to handle.
For more details:
Comments
Post a Comment