~/problems
Problems
Basics first, then the classics, then company-style assessments. Every problem has tests you run right here; multi-level ones unlock as you go. See the roadmap.
Primes, divisors and GCD
Sieve, trial division to sqrt(n), Euclid's algorithm.
Notes
-
Sieve of Eratosthenes, O(n log log n):
is_p = [True] * (n + 1); is_p[0] = is_p[1] = False for i in range(2, int(n ** 0.5) + 1): if is_p[i]: is_p[i*i::i] = [False] * len(range(i*i, n + 1, i)) -
Trial division up to sqrt(n) for factorization. Divisors come in pairs (d, n/d).
-
Euclid:
gcd(a, b) = gcd(b, a % b). Thenlcm = a // gcd(a, b) * b.math.gcdandmath.lcmare built in. -
Smallest prime factor sieve: factorize any number up to n in O(log n).
7 problems
Advanced & competitive
Number theory Modular arithmetic, primes, matrix powers.
Primes, divisors and GCD
- Basics: all divisors up to the square root basics py · c++ · java easy
- Festival lanterns blinking together py · c++ · java easy
- Count Primes py · c++ · java easy
- Split a deck into equal groups py · c++ · java easy
- Divisor counts for many numbers py · c++ · java medium
- Greatest common divisor (Euclid) py · c++ · java easy
- Prime factorization py · c++ · java easy