Professional Documents
Culture Documents
Page
Page
NUMBER THEORY
c Steven & Felix, NUS
In fact, there are only 12 integers less than equal to 36 that are relatively prime to 36. They
are 1, 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, and 35. As we need to factor N , the complexity of
this algorithm is similar with the complexity of factoring an integer with trial division mentioned
earlier. The code is below.
int EulerPhi(int N) {
vi factors = primeFactors(N);
vi::iterator new_end = unique(factors.begin(), factors.end()); // get unique
int result = N;
for (vi::iterator i = factors.begin(); i != new_end; i++)
result = result - result / *i;
return result;
}
1. UVa 10179 - Irreducible Basic Fractions (direct application of Euler’s Phi function)
2. UVa 10299 - Relatives (another direct application of Euler’s Phi function)
3. UVa 10820 - Send A Table (a[i] = a[i - 1] + 2 ×ϕ(i))
4. UVa 11064 - Number Theory
5. UVa 11327 - Enumerating Rational Numbers
99