cpplib

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub morioprog/cpplib

:heavy_check_mark: Euler's totient function (オイラーのφ関数)
(math/prime/euler_totient.hpp)

概要

$1$以上$N$以下の整数について, $N$と互いに素なものの個数を求める.

計算量

$O(\sqrt{n})$

使用例

Verified with

Code

/**
* @brief Euler's totient function (オイラーのφ関数)
* @docs docs/math/prime/euler_totient.md
*/

long long euler_totient(long long n) {
    long long ret = n;
    for (long long i = 2; i * i <= n; ++i) {
        if (n % i) continue;
        ret -= ret / i;
        while (n % i == 0) n /= i;
    }
    if (n > 1) ret -= ret / n;
    return ret;
}
#line 1 "math/prime/euler_totient.hpp"
/**
* @brief Euler's totient function (オイラーのφ関数)
* @docs docs/math/prime/euler_totient.md
*/

long long euler_totient(long long n) {
    long long ret = n;
    for (long long i = 2; i * i <= n; ++i) {
        if (n % i) continue;
        ret -= ret / i;
        while (n % i == 0) n /= i;
    }
    if (n > 1) ret -= ret / n;
    return ret;
}
Back to top page