1

Is it a matter of cutting out iterations to check if a number is a prime?

ex. 37 is a prime, and checking up to 18.5 (half of 37) versus 6.08(square root) cuts out a lot of the work, but both follow the same principle?

sorry for asking, i am trying to solidify my logic of using the square root of a number to determine if it is a prime number, and trying to explain it to others

amit
  • 175,853
  • 27
  • 231
  • 333
  • Because if a > sqrt(N) > 0 and b > sqrt(N) > 0, then a*b > N. This means that if N is factorizable into N=a*b, then at least one in {a,b} must be <= sqrt(N) by contrapositive. – thang Feb 15 '15 at 18:51
  • I'm voting to close this question as off-topic because it is not about programming but about mathematics. – High Performance Mark Feb 16 '15 at 08:15
  • Does this answer your question? [Why do we check up to the square root of a prime number to determine if it is prime?](https://stackoverflow.com/questions/5811151/why-do-we-check-up-to-the-square-root-of-a-prime-number-to-determine-if-it-is-pr) – phuclv Mar 25 '22 at 15:21

2 Answers2

3

It works because if n is divisible by 2 then it is also divisible by n / 2, and if it is not divisible by one, it won't be divisible by the other either. So it's enough to check one of them, and 2 is more convenient to check.

The same logic applies for 3: (lack of) divisibility by 3 implies (lack of) divisibility by n / 3, so it suffices to only check 3.

The same will apply for 4, 5, ..., x. What is x? It's sqrt(n), because n / sqrt(n) = sqrt(n), so things will start repeating after this threshold.

It is enough to check up to and including floor(sqrt(n)). We can prove this:

floor(sqrt(n)) <= ceil(sqrt(n))
For the "=" part, it's obvious both work.
floor(sqrt(n)) < ceil(sqrt(n)) <=> floor(sqrt(n)) + 1 = ceil(sqrt(n))

if n divisible by floor(sqrt(n)) + 1 =>
=> n divisible by n / (floor(sqrt(n)) + 1) < n / floor(sqrt(n))

Since we checked all numbers smaller than or equal to floor(sqrt(n)), we would have found the divisor n / (floor(sqrt(n) + 1)), so there is no point in checking the ceiling.

IVlad
  • 43,099
  • 13
  • 111
  • 179
  • I think what you mean to say is: if n divisible by floor(sqrt(n)) + 1, then n divisible by n / (floor(sqrt(n)) + 1), which is <= floor(sqrt(n)). Since we checked all the numbers <= floor(sqrt(n)), ... – thang Feb 15 '15 at 19:52
  • @thang, right, I don't know how `x` got squeezed in there, thanks. I don't think we need `<=` though. – IVlad Feb 15 '15 at 19:55
0

Square root is preferred because it gives significant improvement in execution time for large numbers.

Why we can use square root as limit? If N is not prime we can represent it as N = p1*p2, where p1 and p2 divisors larger than 1. Obviously, either p1 or p2 (or both) is less or equal to square root of N. So, it's pointless to check any further.

As a note, more advanced methods of checking numbers for primality do exist. For example: Miller-Rabin primality test. While this test is probabilistic, with certain setup it can produce correct answer for all primes less than maximum 64-bit integer.

Ivan Zaitsau
  • 56
  • 1
  • 5