Algorithmsinteger math

Integer Math

TT
Testlaa Team
May 15, 20261 min read

Number theory basics cover divisibility, primes, gcd, and the modulo operator—the vocabulary for every advanced trick.

Why this shows up in the real world

Cryptography, competitive programming, and combinatorics lean on primes, residues mod n, and fast arithmetic on huge integers.

Core idea (explained for students)

Divisibility: a|b iff b % a == 0. Build from: primes, unique factorization, gcd/lcm, congruence mod m.

Try this in Python

def gcd(a: int, b: int) -> int:
    while b:
        a, b = b, a % b
    return a


print(gcd(48, 18))

Common mistakes

  • Treating all odds as prime.
  • Confusing / with divisibility test.
  • Ignoring edge cases 0 and 1.

Key takeaways

  • Drill small examples mod 7 on paper.
  • When stuck, factor numbers involved.

Tags:

Number theoryPythonStudents