From 4944d4c8b1dc00231209d24f26374dfc96d2c429 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Wed, 27 May 2020 14:07:18 -0400 Subject: [PATCH] documentation for eulers_totient_function.cpp --- math/eulers_totient_function.cpp | 62 ++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/math/eulers_totient_function.cpp b/math/eulers_totient_function.cpp index 31ced5a51..e7f6fbf7f 100644 --- a/math/eulers_totient_function.cpp +++ b/math/eulers_totient_function.cpp @@ -1,28 +1,37 @@ -/// C++ Program to find Euler Totient Function -#include - -/* +/** + * @file + * @brief C++ Program to find + * [Euler's Totient](https://en.wikipedia.org/wiki/Euler%27s_totient_function) + * function + * * Euler Totient Function is also known as phi function. - * phi(n) = phi(p1^a1).phi(p2^a2)... - * where p1, p2,... are prime factors of n. - * 3 Euler's properties: - * 1. phi(prime_no) = prime_no-1 - * 2. phi(prime_no^k) = (prime_no^k - prime_no^(k-1)) - * 3. phi(a,b) = phi(a). phi(b) where a and b are relative primes. + * \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. + *
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. + * * Applying this 3 properties on the first equation. - * phi(n) = n. (1-1/p1). (1-1/p2). ... - * where p1,p2... are prime factors. - * Hence Implementation in O(sqrt(n)). - * phi(100) = 40 - * phi(1) = 1 - * phi(17501) = 15120 - * phi(1420) = 560 + * \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$. + *
Some known values are: + * * \f$\phi(100) = 40\f$ + * * \f$\phi(1) = 1\f$ + * * \f$\phi(17501) = 15120\f$ + * * \f$\phi(1420) = 560\f$ */ +#include +#include -// Function to caculate Euler's totient phi -int phiFunction(int n) { - int result = n; - for (int i = 2; i * i <= n; i++) { +/** 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) { n /= i; @@ -34,8 +43,15 @@ int phiFunction(int n) { return result; } -int main() { - int n; +/// 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; std::cout << phiFunction(n); + return 0; }