Modular Subtraction#

Modular subtraction involves subtracting one number from another and then taking the remainder when the result is divided by the modulus.

The formula for modular subtraction of two numbers \(a\) and \(b\) with modulus \(m\) is:

\[ (a - b) \mod m \]

Steps:

  1. Subtract the number \(b\) from \(a\).

  2. If the result is negative, add the modulus \(m\) until the result is non-negative.

  3. Apply the modulus \(m\) to find the remainder.

Functions#

Hide code cell source
import sympy as sp
from sympy import *
from math import gcd
import random
def modular_subtraction(a, b, modulus):
    result = (a - b) % modulus
    print(f"{a} - {b}{result} (mod {modulus})")
    return result

Examples#

Finding \((10 - 17)~mod~7\)#

a = 10
b = 17
modulus = 7

result = modular_subtraction(a, b, modulus)
10 - 17 ≡ 0 (mod 7)

Explanation

To find the result of ( 10 - 17 ) modulo ( 7 ), follow these steps:

  1. Perform the subtraction:

\[ 10 - 17 = -7 \]
  1. Adjust to ensure the result is within the range 0 to modulus-1:

Since we have a negative result, we adjust it by adding the modulus until it becomes non-negative:

\[ -7 + 7 = 0 \]
  1. The adjusted result is the result of the modulo operation:

\[ 10 - 17 \equiv 0 \mod 7 \]

Finding \((217 - 240)~mod~19\)#

a = 217
b = 240
modulus = 19

result = modular_subtraction(a, b, modulus)
217 - 240 ≡ 15 (mod 19)

Finding \((2293 - 334)~mod~38\)#

a = 2293
b = 334
modulus = 38

result = modular_subtraction(a, b, modulus)
2293 - 334 ≡ 21 (mod 38)