Procházet zdrojové kódy

add Euler's function

Olivier Marty před 8 roky
rodič
revize
3562a433b4
3 změnil soubory, kde provedl 47 přidání a 1 odebrání
  1. 28 0
      code/Euler.cpp
  2. 15 0
      code/Euler_test.cpp
  3. 4 1
      main.tex

+ 28 - 0
code/Euler.cpp

@@ -0,0 +1,28 @@
+const int maxn = 100000;
+// Compute phi(n);
+int euler_phi(int n) {
+	int m = (int) sqrt(n + 0.5);
+	int ans = n;
+	for(int i = 2; i <= m; i++)
+    if(n % i ==0) {
+	    ans = ans / i * (i-1);
+      while(n % i ==0)
+        n /= i;
+     }
+	if(n > 1)
+    ans = ans / n * (n-1);
+	return ans;
+}
+// Compute all phi(1), phi(2), ... , phi(n)
+int phi[maxn];
+void phi_table(int n) {
+	for(int i = 2; i <= n; i++)
+    phi[i] = 0;
+	phi[1] = 1;
+	for(int i = 2; i <= n; i++)
+    if(!phi[i])
+  		for(int j = i; j <=n; j += i) {
+  			if(!phi[j]) phi[j] = j;
+  			phi[j] = phi[j] / i *(i-1);
+  		}
+}

+ 15 - 0
code/Euler_test.cpp

@@ -0,0 +1,15 @@
+#include <bits/stdc++.h>
+using namespace std;
+
+#include "Euler.cpp"
+
+int main() {
+  int n = 15;
+  int res[] = {-1,1,1,2,2,4,2,6,4,6,4,10,4,12,6,8};
+	phi_table(n);
+	for (int i = 1; i <= n; i++){
+		assert(phi[i] == euler_phi(i));
+    assert(phi[i] == res[i]);
+	}
+  return 0;
+}

+ 4 - 1
main.tex

@@ -102,13 +102,16 @@ Temps de cuisson : $O(n)$
 % OK ComputeSignedArea
 
 \subsection{Slow Delaunay triangulation}
-{\scriptsize\lstinputlisting{code/Delaunay.cc}} % TODO chercher algo en N (incremental flip)
+{\scriptsize\lstinputlisting{code/Delaunay.cpp}} % TODO chercher algo en N (incremental flip)
 
 \section{Algorithmes numériques}
 
 \subsection{Number theoretic algorithms (modular, Chinese remainder, linear Diophantine)}
 {\scriptsize\lstinputlisting{code/Euclid.cc}}
 
+\subsection{Euler's function}
+{\scriptsize\lstinputlisting{code/Euler.cpp}}
+
 \subsection{Systems of linear equations, matrix inverse, determinant}
 {\scriptsize\lstinputlisting{code/GaussJordan.cc}}