Skip to content

Commit c734e71

Browse files
committed
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.
1 parent 5e51ce8 commit c734e71

1 file changed

Lines changed: 3 additions & 10 deletions

File tree

src/algebra/phi-function.md

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ void phi_1_to_n(int n) {
9494
}
9595
```
9696

97-
9897
## Divisor sum property { #divsum}
9998

10099
This interesting property was established by Gauss:
@@ -124,17 +123,12 @@ void phi_1_to_n(int n) {
124123
phi[j] -= phi[i];
125124
}
126125
```
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.
131126
132-
**Complexity:**
133127
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.
136130
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]$.
138132
139133
140134
```cpp
@@ -177,7 +171,6 @@ void segmented_phi(long long L, long long R) {
177171
}
178172
```
179173

180-
181174
## Application in Euler's theorem { #application }
182175

183176
The most famous and important property of Euler's totient function is expressed in **Euler's theorem**:

0 commit comments

Comments
 (0)