Number theory calculator for primes, GCD and modular arithmetic

Number theory is the study of whole numbers: primes, divisibility and remainders. These free calculators factor numbers into primes, test primality, find the GCD and LCM, work out a mod n and modular inverses, and convert between binary, octal, decimal and hex, showing every step with exact arithmetic.

9 calculatorsBy Hira Noor, mathematicianLast reviewed

Factor a number, test a prime or find a GCD

One number for its prime factors, divisors and φ(n); two to 10 numbers, separated by commas or spaces, for the GCD and LCM. Up to 40 digits each.

Prime factorization of 360
2³ × 3² × 5
360 is composite · 24 positive divisors adding up to 1170 · Euler’s totient φ(360) = 96

Steps: Division steps and every divisor · Primality test and nearest primes. For a mod n, modular powers and inverses, use the modulo calculator.

Calculators in Number Theory & Bases

LCM and GCF Calculator

LCM and GCF of up to 10 numbers with steps: multiples, prime factors, Euclid’s algorithm and a coprime check.

Mod Calculator

a mod n with steps, negative numbers, power mod and modular inverses.

Hex to Decimal Converter

Hex to decimal with place-value steps, plus binary, octal and any base 2 to 36, fractions included.

Binary Calculator

Binary addition, subtraction and bitwise operations with steps, at 8 to 64 bits, signed or unsigned.

Factorial Calculator

Exact n! with steps and every digit, plus digit count, trailing zeros, n!! and Stirling’s approximation.

Which number theory calculator do I need?

Find your task in the first column. Each Try link opens the calculator with that example already filled in, and the result column shows what it returns.

Number theory tasks, an example of each, its result and the calculator to use
TaskExampleResultCalculator
Break a number into prime factors; list its divisors360 Try this factorization23⋅32⋅52^3 \cdot 3^2 \cdot 5, 24 divisorsPrime factorization calculator
Test whether a number is primeIs 221 prime? Try this checkComposite: 221 = 13 × 17Prime number checker
Euler’s totient φ(n)φ(360) Try this totient96Prime factorization calculator
Greatest common divisor and least common multiple252 and 198 Try this GCDGCD 18, LCM 2772GCD and LCM calculator
Remainder of a division, including negative numbers−17 mod 5 Try this remainder3 (but −2 in C, Java and JavaScript)Modulo calculator
Modular power for a huge exponent3200mod133^{200} \bmod 13 Try this power9Modulo calculator
Modular inverse (as in RSA keys)17−1mod312017^{-1} \bmod 3120 Try this inverse2753Modulo calculator
Decimal to binary, with every division by 2−5 as a signed byte Try this conversion1111 1011 (two’s complement)Decimal to binary converter
Binary to decimal, with each bit’s place value1111 1011 Try this conversion251 unsigned, −5 as a signed byteBinary to decimal converter
Convert between bases 2 to 36, fractions includedHex 1A3F to decimal Try this conversion6719Number base converter
Binary arithmetic, bitwise AND, OR, XOR, shifts127 + 1 as a signed 8-bit number Try this sum−128, with an overflow warningBinary calculator
Exact factorial with every digit100! Try this factorial158 digits, 24 trailing zerosFactorial calculator

The prime number checker and the prime factorization calculator overlap. Use the checker for a yes or no answer on numbers up to 200 digits, with the nearest primes on either side. Use the factorization calculator, for numbers up to 40 digits, when you need every prime factor, the divisors, the divisor sum or φ(n).

What is number theory?

Number theory is the branch of mathematics that studies the whole numbers and the relationships between them: which numbers divide which, how every number breaks into primes, and what remainders are left after division. Its central objects are prime numbers, divisibility, the greatest common divisor and modular arithmetic, the arithmetic of remainders.

Its foundation is the fundamental theorem of arithmetic: every whole number greater than 1 is a product of primes in exactly one way, apart from the order of the factors. That is why 360 is always 23⋅32⋅52^3 \cdot 3^2 \cdot 5, whichever factor you divide out first, and it is why the primes are often called the building blocks of the integers.

The subject is old. Euclid proved around 300 BC that there are infinitely many primes and described the GCD algorithm still used today. Pierre de Fermat, often called the father of modern number theory, posed many of its classic problems in the 1600s, and Carl Friedrich Gauss is credited with calling number theory “the queen of mathematics”.

What is number theory used for?

  • Cryptography: RSA encryption multiplies two large primes, encrypts with a modular power and builds the private key from a modular inverse. The 3120 and 17 in the table above come from the textbook RSA example with the primes 61 and 53.
  • Check digits: ISBN-10 book numbers use a check digit calculated modulo 11, and credit card numbers use the Luhn check, which works modulo 10, to catch typing errors.
  • Computing: computers store numbers in binary, programmers write memory addresses in hexadecimal, and hash tables and random number generators rely on remainders and primes.
  • Everyday arithmetic: the LCM gives the common denominator for adding fractions, the GCD reduces a fraction to lowest terms, and clock time is arithmetic modulo 12 or 24.

Is number theory hard? The basic ideas, such as primes, divisibility and remainders, need nothing beyond school arithmetic. University courses become harder because they ask you to prove results rather than compute them. Working small cases by hand and checking each one with a calculator is a good way to build intuition before attempting proofs.

Worked examples with steps

These are the methods the calculators follow for each example. Use the link under each answer to open the same problem, then change the numbers to your own.

Prime factorization of 360

  1. Divide by 2 while you can

    360 ÷ 2 = 180, 180 ÷ 2 = 90, 90 ÷ 2 = 45. 45 is odd, so 2 appears three times.

  2. Move on to 3

    45 ÷ 3 = 15 and 15 ÷ 3 = 5, so 3 appears twice.

  3. Stop at a prime

    5 is prime, so the division ends there.

  4. Count the divisors

    Add 1 to each exponent and multiply: (3 + 1)(2 + 1)(1 + 1) = 24 divisors.

Answer360=23⋅32⋅5360 = 2^3 \cdot 3^2 \cdot 5, with 24 divisors whose sum is 1170. Open it in the prime factorization calculator.

Is 221 a prime number?

  1. Find how far to test

    If 221 has a factor, one of them is at most 221≈14.87\sqrt{221} \approx 14.87, so only the primes 2, 3, 5, 7, 11 and 13 need testing.

  2. Rule out the small primes

    221 is odd, its digits add up to 5 (not a multiple of 3), it does not end in 0 or 5, and dividing by 7 or 11 leaves a remainder.

  3. Try 13

    221 ÷ 13 = 17 exactly.

Answer221 is composite: 221 = 13 × 17. The next prime is 223. Open it in the prime number checker.

GCD and LCM of 252 and 198 with Euclid’s algorithm

  1. Divide the larger by the smaller

    252 = 1 × 198 + 54.

  2. Repeat with the divisor and the remainder

    198 = 3 × 54 + 36, then 54 = 1 × 36 + 18, then 36 = 2 × 18 + 0.

  3. Read off the GCD

    The last non-zero remainder is 18, so gcd(252, 198) = 18.

  4. Find the LCM

    lcm(a,b)=abgcd(a,b)=252⋅19818=2772\operatorname{lcm}(a, b) = \frac{ab}{\gcd(a, b)} = \frac{252 \cdot 198}{18} = 2772.

Answergcd(252, 198) = 18 and lcm(252, 198) = 2772. Running the steps backwards gives the Bézout identity 18 = 252 × 4 + 198 × (−5). Open it in the GCD and LCM calculator.

A modular power: 3²⁰⁰ mod 13

  1. Check the conditions for Fermat’s little theorem

    13 is prime and does not divide 3, so 312≡1(mod13)3^{12} \equiv 1 \pmod{13}.

  2. Reduce the exponent

    200 = 16 × 12 + 8, so 3200=(312)16⋅38≡38(mod13)3^{200} = (3^{12})^{16} \cdot 3^8 \equiv 3^8 \pmod{13}.

  3. Finish with small numbers

    32=93^2 = 9, 34=81≡33^4 = 81 \equiv 3 and 38≡32=9(mod13)3^8 \equiv 3^2 = 9 \pmod{13}.

Answer3200≡9(mod13)3^{200} \equiv 9 \pmod{13}, found without ever writing out the 96-digit number 32003^{200}. The calculator also shows the square-and-multiply table it uses for any modulus. Open it in the modulo calculator.

Convert 156.625 to binary

  1. Convert the whole part

    Divide 156 by 2 repeatedly and keep the remainders: 0, 0, 1, 1, 1, 0, 0, 1. Read them from the bottom up: 10011100.

  2. Convert the fraction

    Multiply 0.625 by 2 repeatedly and keep the whole parts: 1.25 → 1, 0.5 → 0, 1.0 → 1. Read them from the top down: .101.

  3. Join the two parts

    Check: 128 + 16 + 8 + 4 + 0.5 + 0.125 = 156.625.

Answer156.625 = 10011100.101₂. Not every decimal ends in binary: 0.1 repeats forever, which is why 0.1 + 0.2 is not exactly 0.3 on a computer. Open it in the decimal to binary converter.

Number theory formulas quick reference

Common number theory formulas with an example of each
FormulaRuleExample
Division with remaindera=nq+ra = nq + r, with 0≤r<n0 \le r < n125 = 7 × 17 + 6, so 125 mod 7 = 6
Euclid’s algorithmgcd(a,b)=gcd(b,amodb)\gcd(a, b) = \gcd(b, a \bmod b)gcd(252, 198) = gcd(198, 54) = … = 18
GCD and LCMgcd(a,b)⋅lcm(a,b)=ab\gcd(a, b) \cdot \operatorname{lcm}(a, b) = ab18 × 2772 = 252 × 198 = 49,896
Number of divisorsFor n=p1e1⋯pkekn = p_1^{e_1} \cdots p_k^{e_k}: τ(n)=(e1+1)⋯(ek+1)\tau(n) = (e_1 + 1) \cdots (e_k + 1)τ(360) = 4 × 3 × 2 = 24
Euler’s totientφ(n)=n∏p∣n(1−1p)\varphi(n) = n \prod_{p \mid n} \left(1 - \frac{1}{p}\right)φ(360) = 360 × ½ × ⅔ × ⅘ = 96
Fermat’s little theoremap−1≡1(modp)a^{p-1} \equiv 1 \pmod{p} for a prime p that does not divide a312≡1(mod13)3^{12} \equiv 1 \pmod{13}
Modular inversex with ax≡1(modn)ax \equiv 1 \pmod{n}; it exists exactly when gcd(a, n) = 117 × 2753 = 46,801 ≡ 1 (mod 3120)
Trailing zeros of n!⌊n/5⌋+⌊n/25⌋+⌊n/125⌋+⋯\lfloor n/5 \rfloor + \lfloor n/25 \rfloor + \lfloor n/125 \rfloor + \cdots100! ends in 20 + 4 = 24 zeros

What these calculators do not cover yet

There is no Chinese remainder theorem solver, no linear Diophantine equation solver and no continued fractions tool yet, and no Legendre or Jacobi symbols, discrete logarithms or prime list generator. The GCD calculator’s Bézout coefficients give one solution of ax + by = gcd(a, b), which is the starting point for a linear Diophantine equation. Prime factorization stops after about one second on a number whose prime factors are all very large, and says so instead of guessing. For how to type numbers in other bases and use bitwise operators, see the number bases and bitwise operations manual page, and for how results are tested, see the methodology.

About these calculators

Every tool here works with exact whole numbers stored digit by digit, not the 15 to 17 significant digits of an ordinary calculator. The factorial calculator prints all 158 digits of 100!, and the modulo calculator handles numbers of up to 100 digits with exponents of up to 30 digits.

Writing code? The binary calculator handles two’s complement, overflow and bit shifts at a word size of 8, 16, 32 or 64 bits, the decimal to binary converter shows every division by 2, the binary to decimal converter shows the place value of every bit, and the number base converter turns hex such as FF into decimal 255. Watch negative remainders: −17 % 5 is −2 in C, Java and JavaScript but 3 in Python, and the modulo calculator shows both.

All of these number theory calculators are free, need no account and run in your browser, with every step shown and nothing locked. Results are checked against reference values by automated tests, as described in how we verify calculations.

Frequently asked questions

Is this number theory calculator free, and does it show steps?

Yes. Every calculator here is free, needs no account and shows its working: the division ladder for prime factors, the test used for primes, each line of Euclid’s algorithm for the GCD, the square-and-multiply table for modular powers and the repeated divisions for base conversion.

How large can the numbers be?

The prime number checker accepts up to 200 digits, the modulo calculator up to 100 digits, and the prime factorization and GCD calculators up to 40 digits per number. The factorial calculator goes up to 10,000!, which has 35,660 digits. Factoring a large number whose prime factors are all big can take longer than the calculator allows; if so, it stops after about one second and says so.

Is 1 a prime number?

No. A prime has exactly two distinct positive divisors, 1 and itself. The number 1 has only one divisor, so it is neither prime nor composite. Excluding it keeps prime factorizations unique.

How does the calculator know a large number is prime?

Small numbers are checked by trial division. Larger ones go through the Miller–Rabin test with the 13 prime bases from 2 to 41, which is a proof for every number below about 3.3 × 10²⁴. Above that, a strong Lucas test is added (together, the Baillie–PSW test). No composite number is known to pass it, but the result is labeled a probable prime rather than a proven one.

What is the difference between GCD and LCM?

The GCD is the largest number that divides all of your numbers, and the LCM is the smallest number that all of them divide. For 12 and 18, the GCD is 6 and the LCM is 36. For two positive numbers, GCD × LCM equals their product: 6 × 36 = 12 × 18 = 216.

Can it calculate Euler’s totient φ(n)?

Yes. The prime factorization calculator lists φ(n), the count of numbers from 1 to n that share no factor with n, next to the divisor count and divisor sum. For example, φ(360) = 96.

What is a modular inverse used for?

It is the number that “undoes” multiplication when you work with remainders: x is the inverse of a modulo n when ax leaves remainder 1. RSA uses one to turn the public key into the private key, and it is also how you solve congruences such as 17x ≡ 5 (mod 3120). An inverse exists only when a and n share no common factor.

Is there a number theory calculator app?

There is no separate app to install. Every calculator here works in your phone’s browser, and you can add ScientifiCalc to your home screen to open it with one tap, like an app.

Sources and further reading

  1. Number theory (opens in a new tab), Encyclopaedia Britannica
  2. An Introduction to the Theory of Numbers (G. H. Hardy and E. M. Wright, 6th ed.), Oxford University Press
  3. Prime Numbers: A Computational Perspective (R. Crandall and C. Pomerance, 2nd ed.): primality tests and factoring algorithms, Springer
  4. The Art of Computer Programming, Vol. 2: Seminumerical Algorithms (D. E. Knuth, 3rd ed., §4.5.2, the greatest common divisor), Addison-Wesley