0

How to check prime number

21st Feb 2021, 5:59 PM
prem kumar
prem kumar - avatar
6 odpowiedzi
+ 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 .
21st Feb 2021, 6:08 PM
Abhay
Abhay - avatar
+ 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
21st Feb 2021, 7:27 PM
george
george - avatar
+ 1
Hello 👋 prem kumar First let's know your attempt first then we may do that for you
21st Feb 2021, 7:01 PM
Mohd Aadil
Mohd Aadil - avatar
+ 1
Abhay it not necessary to run up to number -1, it is enough to run up to sqrt(number)
22nd Feb 2021, 11:40 AM
george
george - avatar
+ 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 .
22nd Feb 2021, 12:16 PM
Abhay
Abhay - avatar
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.
22nd Feb 2021, 6:05 PM
george
george - avatar