TheAlgorithms-C-Plus-Plus/math/eulers_totient_function.cpp

67 lines
1.6 KiB
C++
Raw Normal View History

/**
* @file
* @brief C++ Program to find
* [Euler's Totient](https://en.wikipedia.org/wiki/Euler%27s_totient_function)
* function
*
2020-04-01 05:26:32 +08:00
* Euler Totient Function is also known as phi function.
* \f[\phi(n) =
* \phi\left({p_1}^{a_1}\right)\cdot\phi\left({p_2}^{a_2}\right)\ldots\f] where
* \f$p_1\f$, \f$p_2\f$, \f$\ldots\f$ are prime factors of n.
* <br/>3 Euler's properties:
* 1. \f$\phi(n) = n-1\f$
* 2. \f$\phi(n^k) = n^k - n^{k-1}\f$
* 3. \f$\phi(a,b) = \phi(a)\cdot\phi(b)\f$ where a and b are relative primes.
*
2020-04-01 05:26:32 +08:00
* Applying this 3 properties on the first equation.
* \f[\phi(n) =
* n\cdot\left(1-\frac{1}{p_1}\right)\cdot\left(1-\frac{1}{p_2}\right)\cdots\f]
* where \f$p_1\f$,\f$p_2\f$... are prime factors.
* Hence Implementation in \f$O\left(\sqrt{n}\right)\f$.
* <br/>Some known values are:
* * \f$\phi(100) = 40\f$
* * \f$\phi(1) = 1\f$
* * \f$\phi(17501) = 15120\f$
* * \f$\phi(1420) = 560\f$
2020-04-01 05:26:32 +08:00
*/
#include <cstdlib>
#include <iostream>
2020-04-01 05:26:32 +08:00
/** Function to caculate Euler's totient phi
*/
uint64_t phiFunction(uint64_t n)
{
uint64_t result = n;
for (uint64_t i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
while (n % i == 0)
{
2020-03-29 20:01:34 +08:00
n /= i;
2020-03-29 19:48:49 +08:00
}
2020-04-01 05:26:32 +08:00
result -= result / i;
2020-03-29 19:48:49 +08:00
}
}
if (n > 1)
result -= result / n;
2020-03-29 20:01:34 +08:00
return result;
2020-03-29 19:48:49 +08:00
}
/// Main function
int main(int argc, char *argv[])
{
uint64_t n;
if (argc < 2)
{
std::cout << "Enter the number: ";
}
else
{
n = strtoull(argv[1], nullptr, 10);
}
std::cin >> n;
2020-03-29 20:09:39 +08:00
std::cout << phiFunction(n);
return 0;
2020-03-29 19:48:49 +08:00
}