Field manual
Math and number theory (GCD, sieve, modular)
variesEuclid's algorithm shrinks the pair (a, b) to (b, a mod b) until the remainder is 0; what's left is the greatest common divisor. Sieves list primes up to n and modular arithmetic keeps huge answers inside a fixed range, the other explicit-math tools in this family.
Signals
reduce a fraction to lowest termslist all primes up to nanswer modulo 1e9+7when do two repeating intervals line up again (LCM)
Template
function gcd(a, b) {
while (b !== 0) {
[a, b] = [b, a % b];
}
return a;
}
function lcm(a, b) {
return (a / gcd(a, b)) * b;
}a, b up to ~1e18 for Euclid's GCD (O(log(min(a,b))) steps), n up to ~1e6..1e7 for a sieve of primes -> O(n log log n); modular exponentiation for a^b mod m runs in O(log b).
Learn this pattern