From 88d5600bc1b01f84fd40548fb95ddf1660d2f182 Mon Sep 17 00:00:00 2001 From: Horace He Date: Tue, 14 Apr 2020 03:44:21 -0400 Subject: [PATCH] tried speculative thing --- content/number-theory/phiFunction.h | 38 +++++++++++++++-------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/content/number-theory/phiFunction.h b/content/number-theory/phiFunction.h index 7724c3e81..5774b3619 100644 --- a/content/number-theory/phiFunction.h +++ b/content/number-theory/phiFunction.h @@ -1,27 +1,29 @@ /** - * Author: HÃ¥kan Terelius - * Date: 2009-09-25 + * Author: + * Date: * License: CC0 - * Source: http://en.wikipedia.org/wiki/Euler's_totient_function - * Description: \emph{Euler's $\phi$} function is defined as $\phi(n):=\#$ of positive integers $\leq n$ that are coprime with $n$. - * $\phi(1)=1$, $p$ prime $\Rightarrow \phi(p^k)=(p-1)p^{k-1}$, $m,n$ coprime $\Rightarrow \phi(mn)=\phi(m)\phi(n)$. - * If $n=p_1^{k_1}p_2^{k_2} ... p_r^{k_r}$ then $\phi(n) = (p_1-1)p_1^{k_1-1}...(p_r-1)p_r^{k_r-1}$. - * $\phi(n)=n \cdot \prod_{p|n}(1-1/p)$. + * Source: + * Description: Multiplicative functions * - * $\sum_{d|n} \phi(d) = n$, $\sum_{1\leq k \leq n, \gcd(k,n)=1} k = n \phi(n)/2, n>1$ - * - * \textbf{Euler's thm}: $a,n$ coprime $\Rightarrow a^{\phi(n)} \equiv 1 \pmod{n}$. + * Euler's totient: $\phi(1) = 1, \phi(p) = p - 1, \phi(ip) = \phi(i)\phi(p)$ + * + * Mobius function: $\mu(1) = 1, \mu(p) = -1, \mu(ip) = 0$ * - * \textbf{Fermat's little thm}: $p$ prime $\Rightarrow a^{p-1} \equiv 1 \pmod{p}$ $\forall a$. * Status: Tested */ #pragma once -const int LIM = 5000000; -int phi[LIM]; - -void calculatePhi() { - rep(i,0,LIM) phi[i] = i&1 ? i : i/2; - for(int i = 3; i < LIM; i += 2) if(phi[i] == i) - for(int j = i; j < LIM; j += i) phi[j] -= phi[j] / i; +vector calcMult(int n) { + vector sieve(n); + vector phi(n), pr; phi[1] = 1; // f(1) + rep(i, 2, n) { + if (!sieve[i]) pr.push_back(i), phi[i] = i - 1; // f(p) + trav(j, pr) { + if (i * j >= n) break; + sieve[i * j] = true; + if (i % j == 0) { phi[i * j] = phi[i] * j; break; } // f(i*p) + phi[i * j] = phi[i] * phi[j]; + } + } + return phi; }