This is a very elegant algorithm and is certainly better than trial division method. The idea behind this algorithm is to first select 2 and strike of all the multiples of because we know that they are composite. Then look for the next number that is not stricken off, it will definitely be a prime and then strike off all the multiples of that number. This goes on until the square of the number we select is greater than the limit that we assigned to it. As you will notice there is no need to go further because all of their multiple will already be stricken off by one or the other prime before it.
To formalise the algorithm, it can be described as follows :
- Select the smallest prime from the list of all numbers upto n.
- Strike off all the multiples of the number upto n.
- Select the next number which is not stricken off
- Remove all of its multiples
- Keep repeating until the number selected in larger than n.
Comments
Post a Comment