summaryrefslogtreecommitdiff
path: root/math/primeSieve.cpp
diff options
context:
space:
mode:
authorkittobi1992 <kittobi1992@users.noreply.github.com>2014-11-21 19:33:12 +0100
committerkittobi1992 <kittobi1992@users.noreply.github.com>2014-11-21 19:33:12 +0100
commitfa83b8c841073b462840260b54593767e597f543 (patch)
tree623edf4774e71dca69314f12d0b61d3d827fda5b /math/primeSieve.cpp
parentf0ca9a96291d0cc2d1af337ca4d6838339f94539 (diff)
Update primeSieve.cpp
fastest prime sieve
Diffstat (limited to 'math/primeSieve.cpp')
-rw-r--r--math/primeSieve.cpp22
1 files changed, 10 insertions, 12 deletions
diff --git a/math/primeSieve.cpp b/math/primeSieve.cpp
index c17b626..82a4cfd 100644
--- a/math/primeSieve.cpp
+++ b/math/primeSieve.cpp
@@ -1,22 +1,20 @@
#include <iostream>
#include <vector>
-vector<int> primeSieve(int n) {
+using namespace std;
+
+typedef unsigned long long ll;
+
+vector<int> primeSieve(ll n) {
vector<int> primes;
vector<bool> isPrime(n,true);
- for(int i = 2; i < n; i+=2) {
- if(i*i <= n) {
- if(isPrime[i]) {
- primes.push_back(i);
- for(int j = 2; i*j < n; j++) {
- isPrime[i*j] = false;
- }
+ for(ll i = 2; i < n; i+=2) {
+ if(isPrime[i]) {
+ primes.push_back(i);
+ if(i*i <= n) {
+ for(ll j = i; i*j < n; j+=2) isPrime[i*j] = false;
}
}
- else {
- if(isPrime[i])
- primes.push_back(i);
- }
if(i == 2)
i--;
}