How can i efficiently find Prime Numbers ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can i efficiently find Prime Numbers ?

Is there any short and efficient algorithm for finding the prime numbers ?

23rd Aug 2018, 2:00 PM
Antreas Spanos
Antreas Spanos - avatar
5 Answers
0
Antreas Spanos I don't have code ready with me but below are few algorithm: 1. check from 2 to n and try to find mod of n with each number.. if modulo is zero, it is not prime.. 2. rather than going from 2 to n, iterate till n/2 only 3. rather than going from 2 to n, iterate till square root of n only 4. check for module by prime numbers only below n... for example, 15 is the case, just check with 2,3,5,7,11,13( primes below 15) P.S. : algorithm 2 is more efficient than 1. 3 is more efficient than 2 and so on...
23rd Aug 2018, 4:24 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Like this if you want a list of successive primes https://code.sololearn.com/c4s3KswioB63/?ref=app
23rd Aug 2018, 2:33 PM
VcC
VcC - avatar
23rd Aug 2018, 2:36 PM
VcC
VcC - avatar
+ 1
For large numbers if you dont need a list the faster known method is this https://code.sololearn.com/cQq22e5PF7wY/?ref=app
23rd Aug 2018, 2:41 PM
VcC
VcC - avatar
0
PHP or Java languages are preferred
23rd Aug 2018, 2:01 PM
Antreas Spanos
Antreas Spanos - avatar