- Advertisement -

- Advertisement -

OHIO WEATHER

Primality test: Difference between revisions


 

Line 4: Line 4:

== Simple methods ==

== Simple methods ==

The simplest primality test is ”[[trial division]]”: given an input number, n,

The simplest primality test is ”[[trial division]]”: given an input number, n,

check whether it is evenly [[divisibility|divisible]] by any [[prime number]] between 2 and sqrt n (i.e. that the division leaves no [[remainder]]). If so, then n is [[Composite number|composite]]. Otherwise, it is prime.Riesel (1994) pp.2-3 In fact, for any divisor psqrt n, there must be another divisor n/p < sqrt n, and therefore looking for divisors smaller than sqrt n is sufficient.

check whether it is [[divisibility|divisible]] by any [[prime number]] between 2 and sqrt n (i.e. the division leaves no [[remainder]]). If so, then n is [[Composite number|composite]]. Otherwise, it is prime.Riesel (1994) pp.2-3 In fact, for any divisor psqrt n, there must be another divisor n/p sqrt n, and therefore looking for divisors sqrt n is sufficient.

For example, consider the number 100, which is evenly divisible by these numbers:

For example, consider the number 100, these numbers:

:2, 4, 5, 10, 20, 25, 50

:2, 4, 5, 10, 20, 25, 50

Note that the largest factor, 50, is half of 100. This holds true for all ”n”: all divisors are less than or equal to ”n”/2.

:1 times 100, ; 2 times 50, ; 4 times 25, ; 5 times 20, ; 10 times 10, ; 20 times 5, ; 25 times 4, ; 50 times 2, ; 100 times 1.

Notice that products past 10 times 10 are the reverse of products which appeared earlier. For example, 5 times 20 and 20 times 5 are the reverse of each other. Note further that of the two divisors, 5 leq sqrt{100} = 10 and 20 geq sqrt{100} = 10. This observation generalizes to all n: all divisor pairs of n contain a divisor less than or equal to sqrt{n}, so the algorithm need only search for divisors less than / equal to sqrt{n} to guarantee detection of all divisor pairs.

:{{math|2 × 50, 4 × 25, 5 × 20, 10 × 10, 20 × 5, 25 × 4, 50 × 2}}

Also notice that 2 is a prime dividing 100, which immediately proves that 100 is not prime. Every positive integer except 1 is divisible by at least one prime number by the [[Fundamental Theorem of Arithmetic]]. Therefore the algorithm need only search for ”prime” divisors less than / equal to sqrt{n}.

Notice that products past 10 × 10 merely repeat numbers which appeared in earlier products, merely [[commutativity|commuted]]. For example, 5 × 20 and 20 × 5 consist of the same numbers in opposite order. This holds true for all ”n”: all unique divisors of ”n” are numbers less than or equal to {{sqrt|”n”}}, so we need not search past that. (In this example, sqrt n = sqrt {100} = 10.)

For another example, consider how this algorithm determines the primality of 17. One has 4 < sqrt{17} < 5, and the only primes leq sqrt{17} are 2 and 3. Neither divides 17, proving that 17 is prime. For a last example, consider 221. One has 14 < sqrt{221} < 15, and the primes leq sqrt{221} are 2, 3, 5, 7, 11, and 13. Upon checking each, one discovers that 221 / 13 = 17, proving that 221 is not prime.

All even numbers greater than 2 can also be eliminated: if an even number can divide ”n”, so can 2.

In cases where it is not feasible to compute the list of primes leq sqrt{n}, it is also possible to simply (and slowly) check all numbers between 2 and sqrt{n} for divisors. A rather simple optimization is to test divisibility by 2 and by just the odd numbers between 3 and sqrt{n}, since divisibility by an even number implies divisibility by 2.

An example is to use trial division to test the primality of 17. We need only test for divisors up to sqrt n, i.e. integers less than or equal to scriptstyle sqrt{17} approx 4.12, namely 2, 3, and 4. 4 can be skipped because it is an even number: if 4 could evenly divide 17, 2 would too, and 2 is already in the list. That leaves 2 and 3. Divide 17 with each of these numbers, and we find that neither divides 17 evenly—both divisions leave a remainder. So, 17 is prime.

This method can improve further. Observe that all primes greater than 3 are of the form {{math|size=100%|1=6”k” ± 1}}, where k is any integer greater than 0. This is because all integers can be expressed as {{mathPrimality test: Difference between revisions

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.