Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Speculative] Tried replacing phiFunction with linear sieve that can calculate mobius as well. #171

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions content/number-theory/phiFunction.h
Original file line number Diff line number Diff line change
@@ -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<int> calcMult(int n) {
vector<char> sieve(n);
vector<int> 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;
}