Skip to main content

Modular Arithmetic

Ayesha
EditReport

Modular Arithmetic

Modular Arithmetic is a system of arithmetic for integers where numbers wrap around after reaching a certain value called the modulus. It is a crucial concept in various fields, especially in cryptography and number theory.

Video Explanationโ€‹

Core Conceptsโ€‹

Modulusโ€‹

  • The modulus mm is the integer at which numbers wrap around.
  • For any integer aa, the expression amodโ€‰โ€‰ma \mod m gives the remainder of the division of aa by mm.

Basic Operationsโ€‹

  1. Addition : (a+b)modโ€‰โ€‰m(a + b) \mod m
    Example: (7+5)modโ€‰โ€‰10=2(7 + 5) \mod 10 = 2

    Code:

    C++โ€‹

    int modular_add(int a, int b, int m) {
    return (a + b) % m;
    }

    Javaโ€‹

    public static int modularAdd(int a, int b, int m) {
    return (a + b) % m;
    }

    Pythonโ€‹

    def modular_add(a, b, m):
    return (a + b) % m

    Time Complexity: O(1)O(1)โ€‹

  2. Subtraction : (aโˆ’b)modโ€‰โ€‰m(a - b) \mod m
    Example: (3โˆ’4)modโ€‰โ€‰5=4(3 โˆ’ 4) \mod 5 = 4

    Code:

    C++โ€‹

    int modular_sub(int a, int b, int m) {
    return (a - b + m) % m; // ensure non-negative result
    }

    Javaโ€‹

    public static int modularSub(int a, int b, int m) {
    return (a - b + m) % m; // ensure non-negative result
    }

    Pythonโ€‹

    def modular_sub(a, b, m):
    return (a - b + m) % m # ensure non-negative result

    Time Complexity: O(1)O(1)โ€‹

  3. Multiplication:: (aร—b)modโ€‰โ€‰m(a \times b) \mod m
    Example: (4ร—3)modโ€‰โ€‰5=2(4 \times 3) \mod 5 = 2

    Code:

    C++โ€‹

    int modular_mul(int a, int b, int m) {
    return (a * b) % m;
    }

    Javaโ€‹

    public static int modularMul(int a, int b, int m) {
    return (a * b) % m;
    }

    Pythonโ€‹

    def modular_mul(a, b, m):
    return (a * b) % m

    Time Complexity: O(1)O(1)โ€‹

  4. Exponentiation: (ab)modโ€‰โ€‰m(a^b) \mod m
    Example: (23)modโ€‰โ€‰5=3(2^3) \mod 5 = 3

    Code:

    C++โ€‹

    int modular_pow(int base, int exp, int mod) {
    int result = 1;
    base = base % mod;
    while (exp > 0) {
    if (exp % 2 == 1) {
    result = (result * base) % mod;
    }
    exp = exp >> 1; // equivalent to exp //= 2
    base = (base * base) % mod;
    }
    return result;
    }


    Javaโ€‹

    public static int modularPow(int base, int exp, int mod) {
    int result = 1;
    base = base % mod;
    while (exp > 0) {
    if ((exp & 1) == 1) {
    result = (result * base) % mod;
    }
    exp >>= 1; // equivalent to exp /= 2
    base = (base * base) % mod;
    }
    return result;
    }


    Pythonโ€‹

    def modular_pow(base, exp, mod):
    result = 1
    base = base % mod
    while exp > 0:
    if (exp % 2) == 1:
    result = (result * base) % mod
    exp //= 2
    base = (base * base) % mod
    return result

    Time Complexity: O(logย b)O( log \ b)โ€‹

Conclusionโ€‹

Modular arithmetic is a powerful tool in mathematics with significant applications in cryptography, computer science, and number theory. Understanding its core operations and properties is essential for working with modern cryptographic systems and algorithms.

๐Ÿง  Quick Quiz

Test Your Understanding

Answer these 3 questions to check what you have learned.

Q1

What is the main topic of this documentation page?

Q2

What should be identified before implementing modular arithmetic?

Q3

How is understanding of modular arithmetic best checked?

Track Your Progress

Done with this topic? Mark it as complete to track your progress.

๐Ÿ’ฌ Discuss this page

Have a question or spot something confusing in "Modular Arithmetic"? Ask below โ€” it's backed by GitHub Discussions, so maintainers get notified like any other GitHub activity.