Your friend has written a program to check whether or not a number n is prime. It is very simple and works by checking if n is divisible by each number from 2 all the way to n − 1 . When you see this, you curse your friend and tell him he's wasting time.
If your friend didn't want to waste time, what is (approximately) the biggest number that he needs to check before he can determine that any number n is prime?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Smart! And very clear explanation.
Log in to reply
let us assume that i want to check for 6. If i divide 6 by root 6 , i get root 6 then how do i know it's prime or not?
Log in to reply
It turns out that 6 is not an integer, in fact it is 2 . 4 4 9 4 9 approximately. We only need to check the integral values below this to check whether 6 is a prime. In fact, the only number we need to check is 2, which immediately tells us that 6 is not a prime.
Log in to reply
@Agnishom Chattopadhyay – then you should give the correct option as [ root n] where [.] represent the floor function
Log in to reply
@A Former Brilliant Member – I could. It seemed redundant to me, though
At first I thought it was 2 1 n , but no! It is actually n . If a number n is not a prime, it can have two factors, say, a and b and n = a b . If both a > n and b > n , then a b > n . Therefore the largest factor should be n .
Not exactly, you should have said:
The largest factor is within n
Log in to reply
The largest factor is within n
Not exactly, you should have said:
For any factor pair of positive integer n, at least one element in the pair will be less than or equal to n
The largest factor of any integer n is n, and n < n
In fact you should only need to check whether n is divisible by all prime numbers in 2 to sqrt(n) inclusive. Having said that - if you had a table of prime numbers available I guess you wouldn't need the programme in the first place! You could still speed it up by only checking for divisibility by 2 and all odd numbers in the range since all primes are odd except 2.
OR you can just try if it divisible on 2 - 3 - 5 - 7 if yes ,then it isn't prime if no , then it is prime
Log in to reply
What about 11,13,17,19,...
Log in to reply
yup, you are correct sorry for late response. It is not that late right :D
Is there something wrong with the above? I found it is quite true. Can someone explain thanks.
If c = a b and WLOG a ≤ b then a ≤ n therefore we only need to check factors up to n as it must contain a factor a that is less than or equal to n
It's called the Sieve of Eratosthenes .
Because all factors come in pairs (squares have a repeated factor), the largest factor you need to check is root n since one of the pair will be at most root n.
It's not quite the same thing. This sieve cancels out multiples of the smallest prime less than n , then cancels out multiples of the second smallest prime less than n , and so on. Which is different from the search for all the whole numbers less than n .
Hmm... This is not the same thing
Problem Loading...
Note Loading...
Set Loading...
It is just okay to check all the way up to n .
Note that the factors occur in pairs ( a , b ) where n = a b .
Now, either a or b is less than or equal to n . If it were not, then their product would be greater than n ⋅ n = n , and n = a b .
Thus, for every factor that is greater than or equal to n , there exists another factor which is less than or equal to n .