You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
combine complexity and usage into single paragraph & move phi from l to r section under the "Finding the totient from 1 to $ using the divisor sum property" section.
Copy file name to clipboardExpand all lines: src/algebra/phi-function.md
+3-10Lines changed: 3 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -94,7 +94,6 @@ void phi_1_to_n(int n) {
94
94
}
95
95
```
96
96
97
-
98
97
## Divisor sum property { #divsum}
99
98
100
99
This interesting property was established by Gauss:
@@ -124,17 +123,12 @@ void phi_1_to_n(int n) {
124
123
phi[j] -= phi[i];
125
124
}
126
125
```
127
-
### Finding the totient from $L$ to $R$ using the [segmented sieve](sieve-of-eratosthenes.md#segmented-sieve) { data-toc-label="Finding the totient from L to R using the segmented sieve" }
128
-
If we need the totient of all numbers between $L$ and $R$, we can use the [segmented sieve](sieve-of-eratosthenes.md#segmented-sieve) approach.
129
-
130
-
The algorithm works by first precomputing all primes up to $\sqrt{R}$ using a [linear sieve](prime-sieve-linear.md) in $O(\sqrt{R})$ time and space. Then, for each number in the range $[L, R]$, we apply the standard φ formula $\phi(n) = n \cdot \prod_{p | n} \left(1 - \frac{1}{p}\right)$ by iterating over the precomputed primes. We maintain a `rem` array to track the remaining unfactored part of each number; if `rem[i] > 1` after processing all small primes, then the number has a large prime factor greater than $\sqrt{R}$, which we handle in a final pass.
131
126
132
-
**Complexity:**
133
127
134
-
- Preprocessing: $O(\sqrt{R})$ time and space for the linear sieve
135
-
- Query: $O((R - L + 1) \log \log R)$ for computing φ over the range
128
+
#### Finding the totient from $L$ to $R$ using the [segmented sieve](sieve-of-eratosthenes.md#segmented-sieve) { data-toc-label="Finding the totient from L to R using the segmented sieve" }
129
+
If we need the totient of all numbers between $L$ and $R$, we can use the [segmented sieve](sieve-of-eratosthenes.md#segmented-sieve) approach.
136
130
137
-
**Usage:** Call `primes= linear_sieve(sqrt(MAX_R) + 1)` once at startup. Then `phi[i - L]` gives $\phi(i)$ for each $i \in [L, R]$.
131
+
The algorithm first precomputes all primes up to $\sqrt{R}$ using a [linear sieve](prime-sieve-linear.md) in $O(\sqrt{R})$ time and space; then for each number in the range $[L, R]$, it applies the standard φ formula $\phi(n) = n \cdot \prod_{p | n} \left(1 - \frac{1}{p}\right)$ by iterating over these primes while maintaining a `rem` array for the unfactored part—if `rem[i] > 1` after processing all small primes, it indicates a large prime factor greater than $\sqrt{R}$ handled in a final pass—so the range computation runs in $O((R - L + 1) \log \log R)$. To use this, call `primes = linear_sieve(sqrt(MAX_R) + 1)` once at startup; then `phi[i - L]` gives $\phi(i)$ for each $i \in [L, R]$.
138
132
139
133
140
134
```cpp
@@ -177,7 +171,6 @@ void segmented_phi(long long L, long long R) {
177
171
}
178
172
```
179
173
180
-
181
174
## Application in Euler's theorem { #application }
182
175
183
176
The most famous and important property of Euler's totient function is expressed in **Euler's theorem**:
0 commit comments