To test whether a number is prime or not, why do we have to test whether it is divisible only up to the square root of that number?
相关问题
- Finding k smallest elements in a min heap - worst-
- binary search tree path list
- High cost encryption but less cost decryption
- How to get a fixed number of evenly spaced points
- Space complexity of validation of a binary search
相关文章
- What are the problems associated to Best First Sea
- Coin change DP solution to keep track of coins
- Algorithm for partially filling a polygonal mesh
- Robust polygon normal calculation
- Algorithm for maximizing coverage of rectangular a
- How to measure complexity of a string?
- Select unique/deduplication in SSE/AVX
- How to smooth the blocks of a 3D voxel world?
So to check whether a number N is Prime or not. We need to only check if N is divisible by numbers<=SQROOT(N). This is because, if we factor N into any 2 factors say X and Y, ie. N=XY. Each of X and Y cannot be less than SQROOT(N) because then, XY < N Each of X and Y cannot be greater than SQROOT(N) because then, X*Y > N
Therefore one factor must be less than or equal to SQROOT(N) ( while the other factor is greater than or equal to SQROOT(N) ). So to check if N is Prime we need only check those numbers <= SQROOT(N).
Any composite number is a product of primes.
Let say
n = p1 * p2
, wherep2 > p1
and they are primes.If
n % p1 === 0
then n is a composite number.If
n % p2 === 0
then guess whatn % p1 === 0
as well!So there is no way that if
n % p2 === 0
butn % p1 !== 0
at the same time. In other words if a composite number n can be divided evenly by p2,p3...pi (its greater factor) it must be divided by its lowest factor p1 too. It turns out that the lowest factorp1 <= Math.square(n)
is always true.Let's say we have a number "a", which is not prime [not prime/composite number means - a number which can be divided evenly by numbers other than 1 or itself. For example, 6 can be divided evenly by 2, or by 3, as well as by 1 or 6].
6 = 1 × 6 or 6 = 2 × 3
So now if "a" is not prime then it can be divided by two other numbers and let's say those numbers are "b" and "c". Which means
a=b*c.
Now if "b" or "c" , any of them is greater than square root of "a "than multiplication of "b" & "c" will be greater than "a".
So, "b" & "c" is always <= square root of "a" to prove the equation "a=b*c".
Because of the above reason, when we test if a number is prime or not, we only check until square root of that number.
If a number
n
is not a prime, it can be factored into two factorsa
andb
:If both
a
andb
were greater than the square root ofn
,a*b
would be greater thann
. So at least one of those factors must be less than or equal to the square root ofn
, and to check ifn
is prime, we only need to test for factors less than or equal to the square root.Let's suppose that the given integer
N
is not prime,Then N can be factorized into two factors
a
andb
,2 <= a, b < N
such thatN = a*b
. Clearly, both of them can't be greater thansqrt(N)
simultaneously.Let us assume without loss of generality that
a
is smaller.Now, if you could not find any divisor of
N
belonging in the range[2, sqrt(N)]
, what does that mean?This means that
N
does not have any divisor in[2, a]
asa <= sqrt(N)
.Therefore,
a = 1
andb = n
and hence By definition,N
is prime....
Further reading if you are not satisfied:
Many different combinations of
(a, b)
may be possible. Let's say they are:(a1, b1), (a2, b2), (a3, b3), ..... , (ak, bk). Without loss of generality, assume ai < bi,
1<= i <=k
.Now, to be able to show that
N
is not prime it is sufficient to show that none of ai can be factorized further. And we also know that ai <=sqrt(N)
and thus you need to check tillsqrt(N)
which will cover all ai. And hence you will be able to conclude whether or notN
is prime....
Let's say
m = sqrt(n)
thenm × m = n
. Now ifn
is not a prime thenn
can be written asn = a × b
, som × m = a × b
. Notice thatm
is a real number whereasn
,a
andb
are natural numbers.Now there can be 3 cases:
In all 3 cases,
min(a, b) ≤ m
. Hence if we search tillm
, we are bound to find at least one factor ofn
, which is enough to show thatn
is not prime.