Tuesday 20 August 2013

optimizing prime number algorithm



I am doing a function to return a list of prime number up to "n", one what to optimize the algorithm is the following:



"The next most obvious improvement would probably be limiting the testing process to only checking if the potential prime can be factored by those primes less than or equal to the square root of the potential prime, since primes larger than the square root of the potential prime will be complementary factors of at least one prime less than the square root of the potential prime.(taken from http://en.wikibooks.org/wiki/Efficient_Prime_Number_Generating_Algorithms)



Can someone explain this in simpler terms with an example?

Thanks


Answer



Let's say you're trying to find primes below 150. Then, what the statement is saying is that you need to look out for the primes below sqrt(150) i.e. [2,3,5,7,11] only. Why's that you say? Well, if there were to be a number above 11, it'll have to multiply by another prime within the list [2,3,5,7,11].



In other words, if 13 were to be that next prime on the primes list, you'll have to multiply 13 by the primes already on the prime list, namely [2,3,5,7,11]. Also, 13*12>150. This automatically means that the number is not prime.


No comments:

Post a Comment

real analysis - How to find $lim_{hrightarrow 0}frac{sin(ha)}{h}$

How to find $\lim_{h\rightarrow 0}\frac{\sin(ha)}{h}$ without lhopital rule? I know when I use lhopital I easy get $$ \lim_{h\rightarrow 0}...