➗ Math
One-line summary: Number theory, combinatorics, and arithmetic tricks — the foundation of competitive programming warm-ups.
Key Topics
| Topic | Key Insight |
|---|---|
| GCD / LCM | Euclidean: gcd(a,b) = gcd(b, a%b) |
| Sieve of Eratosthenes | Find all primes ≤ n in O(n log log n) |
| Modular Arithmetic | (a+b)%m = ((a%m)+(b%m))%m |
| Combinatorics | nCr = n! / (r! * (n-r)!) |
| Trailing zeros in n! | Count factor-5s: ⌊n/5⌋ + ⌊n/25⌋ + ... |
| Perfect square | Math.sqrt(n) % 1 === 0 |
| Fibonacci |
Common Patterns
GCD / LCM
function gcd(a, b) {
return b === 0 ? a : gcd(b, a % b);
}
function lcm(a, b) {
return (a / gcd(a, b)) * b;
}
Sieve of Eratosthenes
function sieve(n) {
const p = new Array(n + 1).fill(true);
p[0] = p[1] = false;
for (let i = 2; i * i <= n; i++) if (p[i]) for (let j = i * i; j <= n; j += i) p[j] = false;
return p;
}
Trailing Zeros
function trailingZeros(n) {
let c = 0;
while (n >= 5) {
n = Math.floor(n / 5);
c += n;
}
return c;
}
Practice Problems
Easy Problems
Related Topics
- School Basics — primes, GCD, factorial
- Bit Manipulation — arithmetic bit tricks
← Back to Home · © sparshjaswal