TTKTheTextKit
Money and Math7 min read

Prime Factorization: How to Find the Prime Factors of Any Number

Prime factorization explained: find the prime factors of a number step by step, count its divisors, and see why 18 digit numbers factor in milliseconds.

By TheTextKit TeamPublished

Prime factorization checked against SymPy: 4,522 numbers, slowest 28 milliseconds, 0 mismatches

A table of three results for the prime factorization calculator. 4,522 numbers of up to 18 digits were compared with SymPy. The slowest single number took 28 milliseconds. There were 0 mismatches.

Prime factorization means writing a whole number as a product of prime numbers. For example, 360 = 2 × 2 × 2 × 3 × 3 × 5, which is shorter as 2³ × 3² × 5. To find it by hand, divide the number by the smallest prime that fits, repeat with the result, and stop when you reach 1. This guide shows the steps, how to read the answer, and why a computer can factor an 18 digit number in a few milliseconds. We also checked our calculator against SymPy on 4,522 numbers.

What prime factorization is

A prime is a whole number above 1 whose only divisors are 1 and itself: 2, 3, 5, 7, 11 and so on. Every other whole number above 1 is a product of primes. According to Wikipedia's article on the fundamental theorem of arithmetic, that product is unique apart from the order of the factors. Its example is 1200 = 2⁴ × 3 × 5², and no other prime factorization of 1200 exists.

The article credits Euclid for the groundwork in his Elements and Gauss for proving the uniqueness part in 1801. It also explains why 1 is not counted as prime: if it were, 2 could be written as 2 × 1, 2 × 1 × 1 and so on, and uniqueness would be lost.

How to find the prime factors step by step

The division method needs nothing but a list of small primes. Here it is for 360:

StepDivideResult
1360 ÷ 2180
2180 ÷ 290
390 ÷ 245
445 ÷ 315
515 ÷ 35
65 ÷ 51
Dividing 360 into prime factors: three 2s, two 3s and one 5 in six division steps

A table of the six division steps that factor 360. 360 divided by 2 is 180, then 90, then 45. 45 divided by 3 is 15, then 5. 5 divided by 5 is 1. The prime factors are three 2s, two 3s and one 5.

Divide by the smallest prime that fits until you reach 1.

The divisors you used, 2, 2, 2, 3, 3 and 5, are the prime factors. Count them to get the exponents: three 2s, two 3s and one 5, so 360 = 2³ × 3² × 5. You get the same answer with a factor tree, where you split the number into any two factors and keep splitting until only primes are left. The tree can look different, but the primes at the bottom are always the same.

Quick divisibility tests

You can skip most trial divisions with a few rules:

Divisible byTest
2The last digit is even
3The digits add up to a multiple of 3
5The last digit is 0 or 5
9The digits add up to a multiple of 9
Divisibility tests: 2 needs an even last digit, 5 a last digit of 0 or 5, and 3 and 9 a digit sum divisible by 3 or 9

A table of four divisibility tests. A number is divisible by 2 if its last digit is even. It is divisible by 3 if its digits add up to a multiple of 3. It is divisible by 5 if it ends in 0 or 5. It is divisible by 9 if its digits add up to a multiple of 9.

Four quick tests that rule out most small factors.

Take 1,001. It's odd, its digit sum is 2, and it doesn't end in 0 or 5, so 2, 3, 5 and 9 are out. Trying 7 gives 143, and 143 = 11 × 13. So 1,001 = 7 × 11 × 13.

When to stop

You never need to test a divisor larger than the square root of what is left. If a number had a factor above its square root, the matching cofactor would be below it, and you would have found that one already. For 1,001 the root is about 31.6, so you only try primes up to 31. If none divides it, the number is prime.

What the factors tell you

Once you have the factorization you can read off a lot without more work.

  • Number of divisors. Add 1 to each exponent and multiply. For 360 that is (3 + 1)(2 + 1)(1 + 1) = 24. A number is a perfect square exactly when every exponent is even.
  • Sum of divisors. For each prime, add its powers from 1 up to the exponent, then multiply the sums. For 360 it is (1 + 2 + 4 + 8)(1 + 3 + 9)(1 + 5) = 15 × 13 × 6 = 1,170.
  • GCD and LCM. Take the lowest power of each shared prime for the greatest common divisor, and the highest power of each prime for the least common multiple. The GCD and LCM Calculator shows this for any list.
  • Simpler fractions. Cancel the primes the numerator and denominator share. The Fraction Calculator does it automatically.

A number whose divisors add up to twice itself is called perfect. In a search of every number below 10,000 the only ones are 6, 28, 496 and 8,128. Our calculator marks them.

NumberDivisors
36024
1,000,00049
999,999,999,999,999,999640
897,612,484,786,617,600103,680
Number of divisors of four numbers, from 24 for 360 up to 103,680 for 897,612,484,786,617,600

A table of the number of divisors of four numbers. 360 has 24 divisors. 1,000,000 has 49. 999,999,999,999,999,999 has 640. 897,612,484,786,617,600 has 103,680.

The count comes straight from the exponents, with no need to list the divisors.

The last row is a number with 12 different primes and 103,680 divisors. The calculator gives you the count and the sum for it instantly, but it lists divisors only when there are 1,000 or fewer, because a list of 103,680 would not help anyone.

Why big numbers are harder

Trial division is fine for 360, but it gets slow when the number is large and has only big factors. Testing every candidate up to the square root of an 18 digit number means up to a billion divisions. So a calculator needs better methods.

Ours combines three. It divides out every prime below 10,000 first. It then tests what is left for primality with the Miller-Rabin test. Wikipedia lists the bound for which testing the first 13 primes as bases is enough: every number below about 3.317 × 10²⁴, from work by Sorenson and Webster in 2015. Our input limit of 18 digits is far below that, so a prime is never a guess. If the leftover is composite, Pollard's rho algorithm, invented by John Pollard in 1975, finds a factor in a number of steps roughly proportional to the square root of the smallest prime factor.

We timed it on a hard case: 94,906,247 × 94,906,249 = 9,007,195,909,437,503, a product of two primes near 95 million. Plain trial division in JavaScript took 442 milliseconds. The calculator took 5.2.

MethodTime for a 16 digit product of two primes
Trial division442 ms
This calculator5.2 ms
Trial division against the calculator on 9,007,195,909,437,503: 442 milliseconds for trial division, 5.2 for the calculator

A table comparing two methods on the number 9,007,195,909,437,503, which is 94,906,247 times 94,906,249. Plain trial division took 442 milliseconds. The prime factorization calculator took 5.2 milliseconds.

The same 16 digit number, two methods.

The gap grows with size. Of 64 products of two 9 digit primes, the slowest took 18.5 milliseconds.

How we checked the calculator

We built a list of 4,522 numbers from 1 to 999,999,999,999,999,999 and had SymPy, a Python library, factor each one. The list wasn't only random numbers. It included primes of every length and their neighbours, semiprimes of two primes with up to 9 digits each, prime squares and cubes, and products of three primes. It also had numbers built only from small primes, Carmichael numbers such as 561 and 1,105, and the strong pseudoprime 3,215,031,751. Those composite numbers pass simple primality tests, so they are the ones that catch mistakes.

For each number we compared the factorization, the prime check, the number and sum of divisors, Euler's totient, and the full list of divisors when it had 1,000 or fewer. Everything matched, so there were 0 mismatches. The slowest single number took 28 milliseconds. We did the same with 3,000 lists for the GCD and LCM, using Python's math functions, with no differences.

Limits

The calculator takes whole numbers up to 18 digits. Commas are accepted only as thousands separators, so 1,000,000 works and 1,00,000 doesn't. Decimals, signs and exponents are reported, not guessed. A number of 1 has no prime factors, and the tool says it is neither prime nor composite. For numbers beyond 18 digits, or for factoring the very large semiprimes used in cryptography, a specialised library is needed: those take far longer than any browser tool should.

Frequently asked questions

What is prime factorization?

Prime factorization is writing a number as a product of prime numbers. For example, 360 = 2 x 2 x 2 x 3 x 3 x 5, or 2^3 x 3^2 x 5. Every whole number from 2 up has exactly one such product, apart from the order of the factors.

Is 1 a prime number?

No. A prime has exactly two divisors, 1 and itself, and 1 has only one divisor. Excluding 1 keeps the factorization of every other number unique, because otherwise you could always multiply in another 1.

How do I count the divisors of a number?

Add 1 to each exponent in the prime factorization and multiply the results. For 360 = 2^3 x 3^2 x 5, that is 4 x 3 x 2 = 24 divisors. No listing is needed.

How do I know when to stop testing divisors?

Stop at the square root of the number. If a number has a factor larger than its square root, the matching factor is smaller than the root, so you would have found it already.

How big a number can the calculator factor?

Up to 18 digits, which is just under one quintillion. Every number in our tests of that size took under 30 milliseconds, and larger numbers are rejected so the answer stays exact.

Factor a number now

Open the Prime Factorization Calculator to get the factors, the division steps, every divisor and a prime check for any number up to 18 digits. For the greatest common divisor and least common multiple of a list, use the GCD and LCM Calculator. Everything runs in your browser, so nothing you type is uploaded.

Try these free tools

100% in your browser

Keep reading

All articles →