Number-Theory Lab
Primality, factorisation, gcd, modular arithmetic, CRT, φ, π(n), Collatz, continued fractions.
How it works
Big-integer number theory with the working shown: Miller–Rabin primality (deterministic up to 3.3·10^24, otherwise with a stated number of rounds), factorisation by trial division and Pollard rho, gcd/lcm with the extended Euclidean table, modular inverse and exponentiation, the Chinese remainder theorem, Euler φ and divisors, prime counting up to 10 million, Collatz sequences and continued fractions.
- Primality: trial division by primes below 100, then Miller–Rabin. Below 3,317,044,064,679,887,385,961,981 the first 13 primes as bases form a proof (Sorenson & Webster 2015), which covers every 64-bit number. Above it, the stated number of rounds uses bases derived from SHA-256 of n (reproducible); a composite passes each round with probability at most 1/4.
- Factorisation: trial division up to 10,000, then Pollard–Brent rho with a fixed iteration budget; factors are checked with Miller–Rabin. If the budget runs out the remaining cofactor is reported as unfactored, never guessed.
- φ(n) = ∏ p^(e−1)(p − 1), σ(n) = ∏ (p^(e+1) − 1)/(p − 1), d(n) = ∏ (e + 1). Carmichael numbers are detected with Korselt's criterion (square-free, composite, p − 1 | n − 1 for every prime factor).
- gcd via the Euclidean algorithm with Bézout coefficients; inverse exists iff gcd(a, m) = 1. CRT merges congruences pairwise and also handles non-coprime moduli (or reports that none exists). π(n) uses an odd-only sieve of Eratosthenes.
Sources
- Sorenson, J.; Webster, J. (2015): Strong pseudoprimes to twelve prime bases (Mathematics of Computation)
- Brent, R. P. (1980): An improved Monte Carlo factorization algorithm (BIT 20)
Use this tool in an app
These free programs for your browser have this tool built in, next to everything else the task needs.
-
Proofline Math notebook
Solve, graph, test and visualise in one notebook — with data, findings and a report.
Related tools
- Erasure-Coding Playground – Reed–Solomon over GF(256): encode data into k + m shards, lose some, rebuild it.
- Floating-Point Explorer – See how IEEE-754 half/single/double store a number: bits, exact value, ULP, rounding error.
How to use the Number-Theory Lab
Test primality, factor big integers, run the extended Euclidean algorithm, modular inverses, the Chinese remainder theorem, prime counts and continued fractions.
What it is for
Number theory comes up in cryptography courses, competition maths, programming puzzles and the occasional "is this number prime?" moment. Ordinary calculators stop at about 16 digits. This lab works with arbitrarily large integers and tells you how it reached each answer.
The analyze operation runs a primality test, factorises the number and lists φ(n), the number and sum of divisors, the divisors themselves and whether n is a Carmichael number. Other operations cover gcd and lcm with Bézout coefficients, modular exponentiation, modular inverses, the Chinese remainder theorem (CRT), prime counting up to 10 million, Collatz sequences and continued fractions of square roots. Inputs can be decimal, 0x hex, 0b binary or expressions such as 2^127-1.
It is not a factoring engine for RSA-sized numbers. Pollard rho runs with a fixed budget, and if that runs out the remaining cofactor is reported as unfactored, never guessed.
Worked example
Enter 561 with analyze. The tool returned:
561 is composite = 3 × 11 × 17; it is a Carmichael number.
It also gave φ = 320, 8 divisors (1, 3, 11, 17, 33, 51, 187, 561) with a sum of 864. A Carmichael number fools the simple Fermat test for every base coprime to it, which is why proper tools use Miller–Rabin.
Next, 2^64+1. The answer was 18446744073709551617 = 274177 × 67280421310721, with the method shown as deterministic Miller–Rabin, followed by the factor search. It took a fraction of a second.
A few more runs:
| Operation | Input | Result |
|---|---|---|
| gcd | 240, 46 | gcd 2, lcm 5520, 2 = 240·(−9) + 46·47 |
| modinv | 17 mod 3120 | 2753 (check: 17 · 2753 ≡ 1) |
| modpow | 4^13 mod 497 | 445 |
| crt | 2 3, 3 5, 2 7 | x ≡ 23 (mod 105) |
| primecount | 1000000 | 78,498 primes |
| cf | 7 | √7 = [2; (1, 1, 1, 4)], period 4 |
How to read the result
- Answer: prime, composite or "probable prime", or the computed value.
- Primality method: trial division, deterministic Miller–Rabin (a proof for every number below about 3.3·10²⁴, which includes all 64-bit numbers) or probabilistic Miller–Rabin with an error bound.
2^127-1came back as a probable prime with error ≤ 4⁻⁴⁰ and a warning that it lies beyond the deterministic limit. - Factors: prime powers. Check
factorisationComplete: if it's false, the last factor is a composite left unfactored. - Bézout coefficients: the x and y in gcd = a·x + b·y, which is exactly what you need for a modular inverse by hand.
- Convergents (continued fractions): the best rational approximations, such as 127/48 for √7.
Tips and common mistakes
- The CRT accepts non-coprime moduli, but they have to be consistent.
1 4and2 6came back as "no solution exists", because the first makes x odd and the second makes it even. - A modular inverse exists only when gcd(a, m) = 1. Run gcd first if the inverse fails.
- For numbers above the deterministic range you can raise the Miller–Rabin rounds. Each round cuts the error bound by a factor of 4.
- The probabilistic bases are derived from SHA-256 of n, so a rerun gives the same result. That's useful when you're writing up a solution.
- The textbook RSA example works directly: e = 17, φ = 3120 gives the private exponent d = 2753.
FAQ
Is "probable prime" good enough?
For 40 rounds the chance that a composite passes is below 4⁻⁴⁰, smaller than the chance of a hardware error. Below 3.3·10²⁴ the result is a proof, not a probability.
Why couldn't it factor my 200-digit number?
Pollard rho finds small and medium factors quickly. Products of two large primes, as in RSA, need far stronger methods such as the number field sieve.
What is the Collatz operation for?
It counts the steps to reach 1 and the highest value along the way. For 27 that's 111 steps with a peak of 9232.
How big can the prime count go?
Up to 10 million, using a sieve of Eratosthenes.
Guide last updated:
For scripts and AI agents
- JSON API:
POST https://numbertheorylab.usefulweb.site/api/v1/calculate(same calculation as this page) - Interface description: OpenAPI 3.1 · AI guide: llms.txt
- MCP server (Streamable HTTP):
https://numbertheorylab.usefulweb.site/mcp· tool namenumber_theory_lab
Example request
curl -s -X POST https://numbertheorylab.usefulweb.site/api/v1/calculate \
-H 'content-type: application/json' \
-d '{"operation":"analyze","a":"561"}'