diff options
| author | Paul Jungeblut <paul.jungeblut@gmail.com> | 2016-11-09 20:39:31 +0100 |
|---|---|---|
| committer | Paul Jungeblut <paul.jungeblut@gmail.com> | 2016-11-09 20:39:31 +0100 |
| commit | ec035ec7395db153834b8ba96b2ce54b597483b0 (patch) | |
| tree | 09c730a8b3da7b86c0cd9ca1870b0a21d79f6ac6 /math | |
| parent | 3409ec5886360d8e7bba623e6d5b56d103779d49 (diff) | |
Adding Kirchhoffs Theorem.
Diffstat (limited to 'math')
| -rw-r--r-- | math/primeSieve.cpp | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/math/primeSieve.cpp b/math/primeSieve.cpp index 37ea12c..656a597 100644 --- a/math/primeSieve.cpp +++ b/math/primeSieve.cpp @@ -1,19 +1,19 @@ // Laufzeit: O(n * log log n) -#define N 100000001 // Bis 10^8 in unter 64MB Speicher. -bitset<N / 2> isPrime; +#define N 100000000 // Bis 10^8 in unter 64MB Speicher. +bitset<N / 2> isNotPrime; inline bool check(int x) { // Diese Methode zum Lookup verwenden. if (x < 2) return false; else if (x == 2) return true; else if (!(x & 1)) return false; - else return !isPrime[x / 2]; + else return !isNotPrime[x / 2]; } -inline int primeSieve(int n) { // Rückgabe: Anzahl der Primzahlen <= n. - int counter = 1; - for (int i = 3; i <= min(N, n); i += 2) { - if (!isPrime[i / 2]) { - for (int j = 3 * i; j <= min(N, n); j+= 2 * i) isPrime[j / 2] = 1; +inline int primeSieve() { // Rückgabe: Anzahl der Primzahlen < N. + int counter = 1; // Die 2, die sonst vergessen würde. + for (int i = 3; i < N; i += 2) { + if (!isNotPrime[i / 2]) { + for (int j = 3 * i; j < N; j+= 2 * i) isNotPrime[j / 2] = 1; counter++; }} return counter; |
