Free beta: 30 days of full access, no card needed.Sign up free

We use necessary cookies to run the site (sign-in and language). The contact and bug-report forms also use Google reCAPTCHA for spam protection, loaded only if you accept. Privacy policy

Field manual

Math and number theory (GCD, sieve, modular)

varies

Euclid'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