This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
algorithms:sieve:eratosthenes [2018/08/09 11:45] jguerin Moved "implementation notes" under "sources". |
algorithms:sieve:eratosthenes [2018/08/20 13:58] (current) jguerin Minor wording tweak. |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Prime Number Generator: Sieve of Eratosthenes | ====== Prime Number Generator: Sieve of Eratosthenes | ||
- | Prime sieves are common devices for generating prime numbers in a given range. These lists can be used for quick verification of relatively small prime numbers (typically | + | Prime sieves((A sieve (Pronounced like " |
- | == Etymology == | ||
- | A sieve((Pronounced like " | ||
===== Source ===== | ===== Source ===== | ||
Line 11: | Line 9: | ||
def sieve(n): | def sieve(n): | ||
- | primes = [True] * (n+1) # | + | primes = [True] * (n+1) # |
primes[0] = primes[1] = False | primes[0] = primes[1] = False | ||
for i in range(2, int(sqrt(n))+1): | for i in range(2, int(sqrt(n))+1): | ||
- | for j in range(i**2, n+1, i): | + | |
- | primes[j] = False | + | |
+ | primes[j] = False | ||
return [i for i in range(len(primes)) if primes[i]] # generate primes from True values | return [i for i in range(len(primes)) if primes[i]] # generate primes from True values | ||
+ | |||
primes = sieve(1000000) | primes = sieve(1000000) | ||
</ | </ | ||
Line 24: | Line 23: | ||
#include < | #include < | ||
#include < | #include < | ||
- | #include < | ||
#include < | #include < | ||
using namespace std; | using namespace std; | ||
+ | |||
+ | const int MAX_PRIMES=100000000; | ||
- | const int MAX_PRIMES=10000000; | + | bitset<MAX_PRIMES+1> nums; // true for each prime |
+ | vector< | ||
- | vector< | + | void sieve(int n) { |
- | bitset< | + | |
nums.set(); | nums.set(); | ||
nums[0] = nums[1] = false; | nums[0] = nums[1] = false; | ||
for(int i=2; i< | for(int i=2; i< | ||
- | for(int j=i*i; j<n+2; j+=i) | + | |
- | nums[j] = false; | + | |
- | + | nums[j] = false; | |
- | vector< | + | |
for(int i=0; i< | for(int i=0; i< | ||
if(nums[i] == true) | if(nums[i] == true) | ||
primes.push_back(i); | primes.push_back(i); | ||
- | |||
- | return primes; | ||
} | } | ||
+ | |||
int main() { | int main() { | ||
- | | + | sieve(MAX_PRIMES); |
+ | |||
return 0; | return 0; | ||
} | } | ||
</ | </ | ||
- | === Implementation Notes === | + | ==== Implementation Notes ==== |
A C++ [[cpp_std_bitset|bitset]](([[http:// | A C++ [[cpp_std_bitset|bitset]](([[http:// | ||
Line 68: | Line 66: | ||
==== Benchmarks ==== | ==== Benchmarks ==== | ||
- | The Python3 implementation | + | This sieve should work reliably for primes up to 10< |
^ %%n%% ^ Python3((Python 3.5.2)) | ^ %%n%% ^ Python3((Python 3.5.2)) | ||
- | ^ 10< | + | ^ 10< |
- | ^ 10< | + | ^ 10< |
- | ^ 10< | + | ^ 10< |
+ | ^ 10< | ||
+ | ==== Example Problems ==== | ||
+ | | Source | Problem | Difficulty((Difficulty rounded to the nearest 0.5.)) | Solutions (Spoilers) | | ||
+ | | Kattis | [[https:// | ||
+ | | Kattis | [[https:// | ||
+ | | Kattis | [[https:// | ||
==== Source ==== | ==== Source ==== | ||
Implementations are from the pseudocode provided in the Wikipedia article on the [[https:// | Implementations are from the pseudocode provided in the Wikipedia article on the [[https:// | ||