+ 1
How can i efficiently find Prime Numbers ?
Is there any short and efficient algorithm for finding the prime numbers ?
5 Respostas
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...
+ 1
Like this if you want a list of successive primes https://code.sololearn.com/c4s3KswioB63/?ref=app
+ 1
like this for small numbers
https://code.sololearn.com/c43v1uUIguLo/?ref=app
+ 1
For large numbers if you dont need a list the faster known method is this
https://code.sololearn.com/cQq22e5PF7wY/?ref=app
0
PHP or Java languages are preferred