Skip to content
DSA in JavaScript
← All patterns

Number Theory

0/15 solved

What it is

Use properties of divisibility, primes and remainders to avoid brute force.

Used for

GCD and LCM, primes, modular arithmetic, huge powers, digit problems.
a, b
48
18

Step 1 of 5

Goal: the greatest common divisor of 48 and 18. Key fact: the answer stays the same if you replace (a, b) with (b, the remainder of a ÷ b). The numbers shrink quickly.

You see

  • GCD, LCM, divisibility, primes
  • Huge exponents or modular arithmetic
  • Digits and arithmetic without built-ins

Learn it in

Euclidean algorithmLeast common multiplePrimality TestSieve of EratosthenesPrime FactorsFast Powering AlgorithmFactorialHorner's MethodSquare Root (Newton's Method)

Template

const gcd = (a, b) => (b === 0 ? a : gcd(b, a % b));const lcm = (a, b) => (a / gcd(a, b)) * b;

Practice problems