Browse Source

move sieve

Olivier Marty 8 years ago
parent
commit
3cce81f9cf
3 changed files with 20 additions and 18 deletions
  1. 0 18
      code/Primes.cc
  2. 17 0
      code/Sieve.cpp
  3. 3 0
      main.tex

+ 0 - 18
code/Primes.cc

@@ -11,24 +11,6 @@ bool IsPrimeSlow (LL x)
   }
   return true;
 }
-// sieve
-const int maxn = 10000005;
-const int maxp = 700000;
-int vis[maxn]; //0 for prime number
-int prime[maxp];
-void sieve(int n){
-	int m = (int)sqrt(n+0.5);
-	memset(vis, 0, sizeof(vis));
-	for (int i = 2; i <= m, i++) if (!vis[i])
-		for (int j = i*i; j <= n; j += i) vis[j] = 1;
-}
-int gen_primes(int n) {
-	sieve(n);
-	int c = 0;
-	for (int i = 2; i <= n; i++) if (!vis[i])
-		prime[c++] = i;
-	return c;
-}
 // Primes less than 1000:
 //   2    3    5    7   11   13   17   19   23   29   31   37
 //  41   43   47   53   59   61   67   71   73   79   83   89

+ 17 - 0
code/Sieve.cpp

@@ -0,0 +1,17 @@
+const int maxn = 10000005;
+const int maxp = 700000;
+int vis[maxn]; //0 for prime number
+int prime[maxp];
+void sieve(int n){
+	int m = (int)sqrt(n+0.5);
+	memset(vis, 0, sizeof(vis));
+	for (int i = 2; i <= m, i++) if (!vis[i])
+		for (int j = i*i; j <= n; j += i) vis[j] = 1;
+}
+int gen_primes(int n) {
+	sieve(n);
+	int c = 0;
+	for (int i = 2; i <= n; i++) if (!vis[i])
+		prime[c++] = i;
+	return c;
+}

+ 3 - 0
main.tex

@@ -150,6 +150,9 @@ Temps de cuisson : $O(n)$
 \subsection{BigInt}
 {\scriptsize\lstinputlisting{code/BigInt.cpp}}
 
+\subsection{Sieve of Eratosthenes}
+{\scriptsize\lstinputlisting{code/Sieve.cpp}}
+
 \section{Graphes}
 
 \subsection{Fast Dijkstra's algorithm}