mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
fix: fit euler's totient to the contribution guidelines (#2447)
* fix: fit euler's totient to the contribution guidelines. * Update math/eulers_totient_function.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update math/eulers_totient_function.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * fix: add more tests notes: i should have added euler's number first * fix: revert description * Update math/eulers_totient_function.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update math/eulers_totient_function.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * chore: apply suggestions from code review * chore: apply suggestions from code review --------- Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
parent
a0227012ec
commit
4b740d464c
@ -1,9 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* @file
|
* @file
|
||||||
* @brief C++ Program to find
|
* @brief Implementation of [Euler's Totient](https://en.wikipedia.org/wiki/Euler%27s_totient_function)
|
||||||
* [Euler's Totient](https://en.wikipedia.org/wiki/Euler%27s_totient_function)
|
* @description
|
||||||
* function
|
|
||||||
*
|
|
||||||
* Euler Totient Function is also known as phi function.
|
* Euler Totient Function is also known as phi function.
|
||||||
* \f[\phi(n) =
|
* \f[\phi(n) =
|
||||||
* \phi\left({p_1}^{a_1}\right)\cdot\phi\left({p_2}^{a_2}\right)\ldots\f] where
|
* \phi\left({p_1}^{a_1}\right)\cdot\phi\left({p_2}^{a_2}\right)\ldots\f] where
|
||||||
@ -23,36 +21,58 @@
|
|||||||
* * \f$\phi(1) = 1\f$
|
* * \f$\phi(1) = 1\f$
|
||||||
* * \f$\phi(17501) = 15120\f$
|
* * \f$\phi(17501) = 15120\f$
|
||||||
* * \f$\phi(1420) = 560\f$
|
* * \f$\phi(1420) = 560\f$
|
||||||
|
* @author [Mann Mehta](https://github.com/mann2108)
|
||||||
*/
|
*/
|
||||||
#include <cstdlib>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
/** Function to caculate Euler's totient phi
|
#include <iostream> /// for IO operations
|
||||||
|
#include <cassert> /// for assert
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Mathematical algorithms
|
||||||
|
* @namespace
|
||||||
|
*/
|
||||||
|
namespace math {
|
||||||
|
/**
|
||||||
|
* @brief Function to calculate Euler's Totient
|
||||||
|
* @param n the number to find the Euler's Totient of
|
||||||
*/
|
*/
|
||||||
uint64_t phiFunction(uint64_t n) {
|
uint64_t phiFunction(uint64_t n) {
|
||||||
uint64_t result = n;
|
uint64_t result = n;
|
||||||
for (uint64_t i = 2; i * i <= n; i++) {
|
for (uint64_t i = 2; i * i <= n; i++) {
|
||||||
if (n % i == 0) {
|
if (n % i != 0) continue;
|
||||||
while (n % i == 0) {
|
while (n % i == 0) n /= i;
|
||||||
n /= i;
|
|
||||||
}
|
|
||||||
result -= result / i;
|
result -= result / i;
|
||||||
}
|
}
|
||||||
}
|
if (n > 1) result -= result / n;
|
||||||
if (n > 1)
|
|
||||||
result -= result / n;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
} // namespace math
|
||||||
|
|
||||||
/// Main function
|
/**
|
||||||
int main(int argc, char *argv[]) {
|
* @brief Self-test implementations
|
||||||
uint64_t n;
|
* @returns void
|
||||||
if (argc < 2) {
|
*/
|
||||||
std::cout << "Enter the number: ";
|
static void test() {
|
||||||
} else {
|
assert(math::phiFunction(1) == 1);
|
||||||
n = strtoull(argv[1], nullptr, 10);
|
assert(math::phiFunction(2) == 1);
|
||||||
|
assert(math::phiFunction(10) == 4);
|
||||||
|
assert(math::phiFunction(123456) == 41088);
|
||||||
|
assert(math::phiFunction(808017424794) == 263582333856);
|
||||||
|
assert(math::phiFunction(3141592) == 1570792);
|
||||||
|
assert(math::phiFunction(27182818) == 12545904);
|
||||||
|
|
||||||
|
std::cout << "All tests have successfully passed!\n";
|
||||||
}
|
}
|
||||||
std::cin >> n;
|
|
||||||
std::cout << phiFunction(n);
|
/**
|
||||||
|
* @brief Main function
|
||||||
|
* @param argc commandline argument count (ignored)
|
||||||
|
* @param argv commandline array of arguments (ignored)
|
||||||
|
* @returns 0 on exit
|
||||||
|
*/
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
test();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user