Skip to main content
edited body
Source Link

ony'sGs_'s answer starts off by essentially finding all the prime factors of n. If you're going to do that, then you might as well use the following well-known formula that calculates the Euler function from the prime factorisation:

phi(p1 ^ a1 * ... * pk ^ ak) =
      (p1 ^ a1 - p1 ^ (a1 - 1))
    * ...
    * (pk ^ ak - pk ^ (ak - 1))

The code would look something like this:

long long phi = 1;

// factorization of n
for (long long i = 2, m = n; m > 1; i++)
{
    // Now phi(n/m) == phi
    if (m % i == 0)
    {
        // i is the smallest prime factor of m, and is not a factor of n/m
        m /= i;
        phi *= (i - 1);
        while (m % i == 0) {
            m /= i;
            phi *= i;
        }
    }
}

return phi;

ony's answer starts off by essentially finding all the prime factors of n. If you're going to do that, then you might as well use the following well-known formula that calculates the Euler function from the prime factorisation:

phi(p1 ^ a1 * ... * pk ^ ak) =
      (p1 ^ a1 - p1 ^ (a1 - 1))
    * ...
    * (pk ^ ak - pk ^ (ak - 1))

The code would look something like this:

long long phi = 1;

// factorization of n
for (long long i = 2, m = n; m > 1; i++)
{
    // Now phi(n/m) == phi
    if (m % i == 0)
    {
        // i is the smallest prime factor of m, and is not a factor of n/m
        m /= i;
        phi *= (i - 1);
        while (m % i == 0) {
            m /= i;
            phi *= i;
        }
    }
}

return phi;

Gs_'s answer starts off by essentially finding all the prime factors of n. If you're going to do that, then you might as well use the following well-known formula that calculates the Euler function from the prime factorisation:

phi(p1 ^ a1 * ... * pk ^ ak) =
      (p1 ^ a1 - p1 ^ (a1 - 1))
    * ...
    * (pk ^ ak - pk ^ (ak - 1))

The code would look something like this:

long long phi = 1;

// factorization of n
for (long long i = 2, m = n; m > 1; i++)
{
    // Now phi(n/m) == phi
    if (m % i == 0)
    {
        // i is the smallest prime factor of m, and is not a factor of n/m
        m /= i;
        phi *= (i - 1);
        while (m % i == 0) {
            m /= i;
            phi *= i;
        }
    }
}

return phi;
removed redundant variable `wheel`
Source Link

ony's answer starts off by essentially finding all the prime factors of n. If you're going to do that, then you might as well use the following well-known formula that calculates the Euler function from the prime factorisation:

phi(p1 ^ a1 * ... * pk ^ ak) =
      (p1 ^ a1 - p1 ^ (a1 - 1))
    * ...
    * (pk ^ ak - pk ^ (ak - 1))

The code would look something like this:

long long wheel = 1;
long long phi = 1;

// factorization of n
for (long long i = 2, m = n; m > 1; i++)
{
    // Now phi(n/m) == phi
    if (m % i == 0)
    {
        // i is the smallest prime factor of m, and is not a factor of n/m
        m /= i;
        phi *= (i - 1);
        while (m % i == 0) {
            m /= i;
            phi *= i;
        }
    }
}

return phi;

ony's answer starts off by essentially finding all the prime factors of n. If you're going to do that, then you might as well use the following well-known formula that calculates the Euler function from the prime factorisation:

phi(p1 ^ a1 * ... * pk ^ ak) =
      (p1 ^ a1 - p1 ^ (a1 - 1))
    * ...
    * (pk ^ ak - pk ^ (ak - 1))

The code would look something like this:

long long wheel = 1;
long long phi = 1;

// factorization of n
for (long long i = 2, m = n; m > 1; i++)
{
    // Now phi(n/m) == phi
    if (m % i == 0)
    {
        // i is the smallest prime factor of m, and is not a factor of n/m
        m /= i;
        phi *= (i - 1);
        while (m % i == 0) {
            m /= i;
            phi *= i;
        }
    }
}

return phi;

ony's answer starts off by essentially finding all the prime factors of n. If you're going to do that, then you might as well use the following well-known formula that calculates the Euler function from the prime factorisation:

phi(p1 ^ a1 * ... * pk ^ ak) =
      (p1 ^ a1 - p1 ^ (a1 - 1))
    * ...
    * (pk ^ ak - pk ^ (ak - 1))

The code would look something like this:

long long phi = 1;

// factorization of n
for (long long i = 2, m = n; m > 1; i++)
{
    // Now phi(n/m) == phi
    if (m % i == 0)
    {
        // i is the smallest prime factor of m, and is not a factor of n/m
        m /= i;
        phi *= (i - 1);
        while (m % i == 0) {
            m /= i;
            phi *= i;
        }
    }
}

return phi;
Source Link

ony's answer starts off by essentially finding all the prime factors of n. If you're going to do that, then you might as well use the following well-known formula that calculates the Euler function from the prime factorisation:

phi(p1 ^ a1 * ... * pk ^ ak) =
      (p1 ^ a1 - p1 ^ (a1 - 1))
    * ...
    * (pk ^ ak - pk ^ (ak - 1))

The code would look something like this:

long long wheel = 1;
long long phi = 1;

// factorization of n
for (long long i = 2, m = n; m > 1; i++)
{
    // Now phi(n/m) == phi
    if (m % i == 0)
    {
        // i is the smallest prime factor of m, and is not a factor of n/m
        m /= i;
        phi *= (i - 1);
        while (m % i == 0) {
            m /= i;
            phi *= i;
        }
    }
}

return phi;