How can I decide that a number is prime number? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can I decide that a number is prime number?

I have to ask for a number with "var n=prompt" and after that I have to determine that that number is prim or not. I have been learning cycles in Javascript and I didn't find any answer for that.

13th Nov 2018, 2:58 PM
Bence
Bence - avatar
2 Answers
+ 4
the most basic way is to iterate from one to that number continually dividing by that number.if there are no divisors,its a prime. var is_prime=true for(var i=2;i<num;i++) if(num%i===0) is_prime=false; console.log(is_prime); this is very inefficient for large primes like 224346557567
13th Nov 2018, 3:04 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
0
Thank you for your help! :)
13th Nov 2018, 4:09 PM
Bence
Bence - avatar