ArticleslgStudy

mathematics

Trial division

Trial division is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Trial division rather than just read about it. In short: Trial division is the most laborious but easiest to understand of the integer factorization algorithms. The essential idea behind trial division tests to see if an integer n, the integer to be factored, can be divided by each number in turn that is less than or equal to the square root of n.

Key takeaways

  • Trial division belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Trial division to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Trial division from memory before moving on to harder problems.

Reference excerpt

Trial division is the most laborious but easiest to understand of the integer factorization algorithms. The essential idea behind trial division tests to see if an integer n, the integer to be factored, can be divided by each number in turn that is less than or equal to the square root of n. For example, to find the prime factors of n = 70, one can try to divide 70 by successive primes: first, 70 / 2 = 35; next, neither 2 nor 3 evenly divides 35; finally, 35 / 5 = 7, and 7 is itself prime. So 70 = 2 × 5 × 7. Trial division was first described by Fibonacci in his book Liber Abaci (1202).

Method Given an integer n (n refers to "the integer to be factored"), the trial division consists of systematically testing whether n is divisible by any smaller number. Since the goal is to find a prime factorization, once a prime factor p is found, one can repeat the process with n/p to find another prime factor, and so on. The process terminates when all of the prime factors are found. If n is composite, then its smallest prime factor must be at most n, because it is not possible for two prime factors of n both to be greater than this. Thus, to find the first prime factor, one can proceed by testing each number from 2 up to n whether it evenly divides n. If such a factor is found, we proceed as above with n/p. If every prime factor of n exceeds n, it means that n is prime, and so the process terminates. During the trial divisions, it is only necessary to test against primes, and only those primes less than or equal to n. As a practical matter, it may not be feasible to keep track of all primes less than this bound, but sieving can still reduce the number of non-primes one must test (for example, not testing any even number greater than two, or multiple of three greater than three). It is not strictly necessary to keep track of a table of primes however: if an integer p is found that divides the current integer n to be factored, then it is guaranteed to be prime, because the smaller primes have been excluded as potential factors of n, provided the implementation scans the integers in order starting at 2. Moreover, an additional optimization of the method is as follows. When a prime factor of n is found, instead of immediately repeating the full scan with the new integer n/p all over again beginning at 2, the scan can instead proceed beginning at the prime p. This will then recursively remove from n all powers of the prime p that divide it, and then proceed to the next prime. A simple pseudocode implementation is:

Input: integer n ≥ 2 Output: list of prime factors, including repetitions

factors ← empty list (m is the integer we are working with) m ← n

(Remove factors of two) while 2 divides m: append 2 to factors m ← m / 2

(Beginning at p = 3, remove each prime power that divides m) p ← 3 while p² ≤ m: while p divides m: append p to factors m ← m / p (only need to consider odd p; an optimized version would use a wheel here; cf. Crandall and Pomerance, p. 126) p ← p + 2

if m > 1: (all primes ≤ √m have been removed, so m is prime) append m to factors

return factors

Speed In the worst case, trial division is a laborious algorithm. For a number n having ν bits, if it starts from two and works up only to the square root of n, the algorithm requires

π(n)≈π(2ν/2)≈2ν/2(ν2)ln⁡2 trial divisions, where π(x) denotes the prime-counting function, the number of primes less than x. This does not take into account the overhead of maintaining a table of primes or sieving for them. A useful table need not be large: P(3512) = 32749, the last prime that fits into a sixteen-bit signed integer and P(6542) = 65521 for unsigned sixteen-bit integers. That would suffice to test primality for numbers up to 655372 = 4,295,098,369. Preparing such a table (usually via the Sieve of Eratosthenes) may be especially worthwhile if many numbers are to be tested. If instead a variant is used without primality testing, but simply dividing by every odd number less than the square root of n, prime or not, it can take up to about:

n≈2ν/2 In both cases, the required time grows exponentially with the digits of the number. For many integers, this can be a satisfactory method of factorization because it can detect small factors early, but for integers containing only large prime factors its worst-case exponential growth makes it far inferior to subexponential algorithms. Early-terminating optimized trial division may be used in conjunction with other methods for pulling off small factors. For n chosen uniformly at random from integers of a given length, there is a 50% chance that 2 is a factor of n and a 33% chance that 3 is a factor of n, and so on. It can be shown that 88% of all positive integers have a factor under 100 and that 92% have a factor under 1000. Thus, when confronted by an arbitrary large n, it is worthwhile to check for divisibility by the small primes, since for n=1000, in base-2 ν=10. However, many-digit numbers that do not have factors in the small primes can require days or months to factor with trial division. In such cases other methods are used such as the quadratic sieve and the general number field sieve (GNFS). Because these methods also have superpolynomial time growth a practical limit of ν bits is reached very quickly. For this reason, in public key cryptography, values for n are chosen to have large prime factors of similar size so that they cannot be factored by any publicly known method in a useful time period on any available computer system or computer cluster such as supercomputers and computer grids. The largest cryptography-grade number that has been factored is RSA-260, a 260-digit number, factored in September 2026 using a GPU-accelerated implementation of the general number field sieve.

References

Childs, Lindsay N. (2009). A concrete introduction to higher algebra. Undergraduate Texts in Mathematics (3rd ed.). New York, NY: Springer-Verlag. ISBN 978-0-387-74527-5. Zbl 1165.00002. Crandall, Richard; Pomerance, Carl (2005). Prime numbers. A computational perspective (2nd ed.). New York, NY: Springer-Verlag. ISBN 0-387-25282-7. Zbl 1088.11001.

External links Wikiversity offers a lesson on prime factorization using trial division with Python. Fast JavaScript Prime Factor Calculator using trial division. Can handle numbers up to about 253 Trial Division in Java, C and JavaScript (in Portuguese)

Worked examples

Example 1 — a first encounter with Trial division

Start with the simplest possible case. Write down what Trial division claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Trial division before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Trial division ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Trial division

In research
Trial division appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Trial division in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Trial division is common in secondary-school and first-year university syllabi. It links to neighbouring topics Division (mathematics), Integer factorization algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Trial division outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Trial division” →

How to study Trial division in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Trial division means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Trial division out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Trial division in simple terms?

Trial division is the most laborious but easiest to understand of the integer factorization algorithms. The essential idea behind trial division tests to see if an integer n, the integer to be factored, can be divided by each number in turn that is less than or equal to the square root of n.

Why does Trial division matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Trial division?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Trial division.

Tags

  • Division (mathematics)
  • Integer factorization algorithms

Keep exploring