All tools

Combinatorics and number theory calculator

nPr, nCr, factorials, primes, factorisation, GCD with steps and modular arithmetic.

Whole numbers. Results are exact, however many digits they have.

Results

n! (arrangements of n items)
3 628 800
nPr: ordered, no repetition
720
nCr: unordered, no repetition
120
nʳ: ordered, with repetition
1 000
C(n + r − 1, r): unordered, with repetition
220
Catalan number Cₙ
16 796
Fibonacci number Fₙ
55
Stirling S(n, k): split into k groups
9 330
Stirling c(n, k): permutations with k cycles
1 172 700

What this tool does

It answers the counting and divisibility questions of school and university maths exactly, with whole numbers of any length (BigInt arithmetic, no rounding). Three tabs:

  • Counting: n!, permutations nPr, combinations nCr, the versions with repetition (nʳ and C(n + r − 1, r)), the Catalan number Cₙ, the Fibonacci number Fₙ and the Stirling numbers S(n, k) and c(n, k). Long results show their digit count and copy in full.
  • Primes and factors: whether a number is prime, its prime factorisation, Euler's φ(n), the number and sum of its divisors and the list of divisors, plus all primes in a range.
  • GCD and modular: greatest common divisor and least common multiple, the Bézout coefficients with every step of the extended Euclidean algorithm, a modular power aᵉ mod m and a modular inverse.

Permutations or combinations?

Ask two questions: does order matter, and can an item be chosen again?

No repetitionWith repetition
Order mattersnPr = n!/(n − r)!nʳ
Order does not matternCr = n!/(r!(n − r)!)C(n + r − 1, r)

Gold, silver and bronze among 8 runners: order matters, nobody wins twice, so 8P3 = 336. Three different toppings out of 10: order does not matter, C(10, 3) = 120; if double cheese is allowed, C(12, 3) = 220. A 4-letter code from 26 letters with repeats allowed: 26⁴ = 456,976. A Mega 6/45 lottery ticket: C(45, 6) = 8,145,060 possible draws.

Catalan numbers count balanced bracket strings, binary trees and many other structures (C₁₀ = 16,796). Stirling numbers of the second kind count the ways to split n labelled items into k non-empty groups: 10 people into 3 teams can be split S(10, 3) = 9,330 ways (ignoring team names). Stirling numbers of the first kind count permutations by their number of cycles.

Primes: what "prime" and "probable prime" mean here

Small numbers are proved prime by trial division. Up to 3.3 × 10²⁴ the tool runs Miller–Rabin with the 13 prime bases up to 41, which is a proof in that range (Sorenson and Webster, 2015): strong pseudoprimes such as 3,215,031,751 (which fools bases 2, 3, 5 and 7) are caught. Above that it uses the Baillie–PSW test, Miller–Rabin base 2 plus a strong Lucas test. No composite number is known to pass it, but nobody has proved none exists, so the tool says probable prime. A "composite" verdict is always certain.

Factorisation and its limits

Factoring first divides out primes up to 10,000, then uses Pollard's rho (Brent's variant), which finds a factor p in roughly √p steps. That makes quick work of 600,851,475,143 = 71 × 839 × 1471 × 6857 or of Cole's famous 2⁶⁷ − 1 = 193,707,721 × 761,838,257,287. It stops after two seconds and shows what is left in brackets when the number is a product of two large primes, say two 20-digit primes. That is not a bug: multiplying primes is easy and undoing it is hard, and RSA encryption depends on that gap. Real RSA moduli have over 600 digits, far beyond any browser.

GCD, Bézout and modular inverses

The extended Euclidean table shows each quotient and remainder and the running coefficients. For 240 and 46: the remainders are 10, 6, 4, 2, 0, so the GCD is 2 and 240 × (−9) + 46 × 47 = 2. A modular inverse a⁻¹ mod m exists exactly when gcd(a, m) = 1: 3 × 4 = 12 ≡ 1 (mod 11), so 3⁻¹ mod 11 = 4. Modular powers use square-and-multiply, so 4¹³ mod 497 = 445 and even huge exponents are instant. For the intuition behind all this, read permutations vs combinations, and why factoring is hard; for statistics on your results, the statistics calculator is next door.

Frequently asked questions

What is the difference between a permutation and a combination?

In a permutation the order matters (first, second, third place); in a combination it does not (a hand of cards). 8P3 = 336 podiums from 8 runners, but C(10, 3) = 120 sets of 3 toppings from 10.

How big can the numbers be?

Counting works up to n = 10,000 (10,000! has 35,660 digits). Prime tests and factoring accept up to 300 digits; factoring stops after two seconds.

Is a probable prime really prime?

Below 3.3 × 10²⁴ the answer is proved. Above it the Baillie–PSW test is used: no counterexample is known, but it is not a mathematical proof.

Why can't it factor some numbers?

Pollard's rho needs about √p steps to find a prime factor p. A product of two primes of 20 digits or more needs far longer than the two-second budget. This difficulty is what keeps RSA secure.

When does a modular inverse exist?

Exactly when a and m have no common factor (gcd(a, m) = 1). The tool finds it with the extended Euclidean algorithm.