Lably
Skip to content

Trial division

Integer factorization algorithm

  • 5 min read
  • 2 chapters

Related on Lably

Through Integer factorization, Square root and Fibonacci

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. Clearly, it is only worthwhile to test candidate factors less than n, and in order from two upwards because an arbitrary n is more likely to be divisible by two than by three, and so on. With this ordering, there is no point in testing for divisibility by four if the number has already been determined not divisible by two, and so on for three and any multiple of three, etc. Therefore, the effort can be reduced by selecting only prime numbers as candidate factors, provided they are repeatedly tested until they fail. Furthermore, the trial factors need go no further than n because if n is composite, it must have at least one factor ≤ (n), which would have been detected earlier; having all factors > (n) is impossible because the product of any two would be > n.

When a trial division is successful, the process is applied recursively to the resulting quotient.

A definite bound on the prime factors is possible. Suppose Pi is the i'th prime, so that P1 = 2, P2 = 3, P3 = 5, etc. Then the last prime number worth testing as a possible factor of n is Pi where P2i + 1 > n; equality here would mean that Pi + 1 is a factor. Thus, testing with 2, 3, and 5 suffices up to n = 48 not just 25 because the square of the next prime is 49, and below n = 25 just 2 and 3 are sufficient. Should the square root of n be an integer, then it is a factor and n is a perfect square.

The trial division algorithm in pseudocode:

<b id="mwRQ">algorithm</b> trial-division <b id="mwRg">is</b> <b id="mwRw">input:</b> Integer <i id="mwSA">n</i> to be factored <b id="mwSQ">output:</b> List <i id="mwSg">F</i> of prime factors of <i id="mwSw">n</i> <i id="mwTA">P</i> ← set of all primes <span typeof="mw:Entity" id="mwTQ">≤</span> <span class="mwe-math-element mwe-math-element-inline" typeof="mw:Extension/math" about="#mwt28" data-mw='{"name":"math","attrs":{},"body":{"extsrc":"{{\\sqrt{n}}}"}}' id="mwTg"><span class="mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" class="mathjax_ignore" alttext="{\displaystyle {\sqrt {n}}}"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <mrow class="MJX-TeXAtom-ORD"> <msqrt> <mi>n</mi> </msqrt> </mrow> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle {\sqrt {n}}}</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/2a2994734eae382ce30100fb17b9447fd8e99f81" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -1.005ex; width:3.331ex; height:3.009ex;" alt="{\displaystyle {\sqrt {n}}}"/></span> <i id="mwTw">F</i> ← empty list of factors <b id="mwUA">for each</b> prime <i id="mwUQ">p</i> <b id="mwUg">in</b> <i id="mwUw">P</i> <b id="mwVA">do</b> <b id="mwVQ">while</b> <i id="mwVg">n</i> <b id="mwVw">mod</b> <i id="mwWA">p</i> is 0 Add factor <i id="mwWQ">p</i> to list <i id="mwWg">F</i> <i id="mwWw">n</i> ← <i id="mwXA">n</i>/<i id="mwXQ">p</i> <b id="mwXg">if</b> <i id="mwXw">F</i> is empty <i id="mwYA">(Original n is prime?)</i> Add factor <i id="mwYQ">n</i> to list <i id="mwYg">F</i>

Determining the primes less than or equal to n is not a trivial task as n gets larger, so the simplest computer programs to factor a number just try successive integers, prime and composite, from 2 to n as possible factors.

Sign in to keep reading

One more chapter is free with a Lably account.

More in Algebra