This [Live Example][1]Live Example compares the 2 with a significant difference in execution times.
The sieve, based on the Sieve of Eratosthenes, basically uses an array of booleans initialized to false. You start with the first prime,2, and change every successive boolean that's a multiple to true. Repeat with every element that is false when the loop gets there. When the loop is done, the index of every element that is false is a prime. What I've done is added an optimization and added the primes to the list in the main loop then used another loop that starts where that one left off. The trick is to use booleans so that you're only checking true/false instead of decoding an integer. [1]: http://coliru.stacked-crooked.com/a/cc77df74f1645834
This [Live Example][1] compares the 2 with a significant difference in execution times.
The sieve, based on the Sieve of Eratosthenes, basically uses an array of booleans initialized to false. You start with the first prime,2, and change every successive boolean that's a multiple to true. Repeat with every element that is false when the loop gets there. When the loop is done, the index of every element that is false is a prime. What I've done is added an optimization and added the primes to the list in the main loop then used another loop that starts where that one left off. The trick is to use booleans so that you're only checking true/false instead of decoding an integer. [1]: http://coliru.stacked-crooked.com/a/cc77df74f1645834
This Live Example compares the 2 with a significant difference in execution times.
The sieve, based on the Sieve of Eratosthenes, basically uses an array of booleans initialized to false. You start with the first prime,2, and change every successive boolean that's a multiple to true. Repeat with every element that is false when the loop gets there. When the loop is done, the index of every element that is false is a prime. What I've done is added an optimization and added the primes to the list in the main loop then used another loop that starts where that one left off. The trick is to use booleans so that you're only checking true/false instead of decoding an integer.
This was ported from a C# method I found. I can't remember where though. My apologies to the originator.
The sieve, based on the Sieve of Eratosthenes, basically uses an array of booleans initialized to false. You start with the first prime,2, and change every successive boolean that's a multiple to true. Repeat with every element that is false when the loop gets there. When the loop is done, the index of every element that is false is a prime. What I've done is added an optimization and added the primes to the list in the main loop then used another loop that starts where that one left off. The trick is to use booleans so that you're only checking true/false instead of decoding an integer. [1]: http://coliru.stacked-crooked.com/a/cc77df74f1645834
This was ported from a C# method I found. I can't remember where though. My apologies to the originator. [1]: http://coliru.stacked-crooked.com/a/cc77df74f1645834
This was ported from a C# method I found. I can't remember where though. My apologies to the originator.
The sieve, based on the Sieve of Eratosthenes, basically uses an array of booleans initialized to false. You start with the first prime,2, and change every successive boolean that's a multiple to true. Repeat with every element that is false when the loop gets there. When the loop is done, the index of every element that is false is a prime. What I've done is added an optimization and added the primes to the list in the main loop then used another loop that starts where that one left off. The trick is to use booleans so that you're only checking true/false instead of decoding an integer. [1]: http://coliru.stacked-crooked.com/a/cc77df74f1645834
This was ported from a C# method I found. I can't remember where though. My apologies to the originator. [1]: http://coliru.stacked-crooked.com/a/cc77df74f1645834
This was ported from a C# method I found. I can't remember where though. [1]: http://coliru.stacked-crooked.com/a/cc77df74f1645834
This was ported from a C# method I found. I can't remember where though. My apologies to the originator. [1]: http://coliru.stacked-crooked.com/a/cc77df74f1645834