0
How to check prime number
6 Respostas
+ 3
If a number is divisible by 1 and itself then only it is primary , so try dividing it by others number from 2 to one less than number itself and if it gets divided by them it isn't primary.
Now can you show us your attempt so we can actually help you ?
Between make use of search bar before asking a question as others might have already asked about it before.
And just saying ,you can google all that so i don't see why people keep asking such basic questions again and again .
+ 2
For loop from 2 up to sqrt(n). If your number%i==0 where number is your number and i is num from loop than this is not prime number
function isPrime(n){
const m=Math.sqrt(n)
for(let i=2 ;i<m+1;++i){
if(0==n%i) {
return false;
}
}
return true
}
it is no javascript but you can write on python
+ 1
Hello 👋 prem kumar
First let's know your attempt first then we may do that for you
+ 1
Abhay it not necessary to run up to number -1, it is enough to run up to sqrt(number)
+ 1
george Thank you . Also there is another way to do it as well ,
if(num%2==0 || num%3==0)
return true
i=5
while(i*i<=num) {
if(num%i==0 || num%(i+2) ==0)
return true;
i+=6
}
return false;
And this is the solution i came across when searching for some programs few days ago , they said it is most optimised one .
0
Abhay ouu. It is great i*i is same as sqrt(num) as i say,but cheking with i, i+2 and then make i+=6? It is real optimized.