From 7be8986c118686144d1d633a768017463f984b91 Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Sun, 29 Mar 2020 17:18:49 +0530 Subject: [PATCH 1/6] Added eulers totient function in math --- math/eulers_totient_function.cpp | 48 ++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 math/eulers_totient_function.cpp diff --git a/math/eulers_totient_function.cpp b/math/eulers_totient_function.cpp new file mode 100644 index 000000000..1d1d75edf --- /dev/null +++ b/math/eulers_totient_function.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; +typedef long long int ll; + +/** + +Euler Totient Function also know as phi function. + +phi(n) = phi(p1^a1).phi(p2^a2)... +where p1, p2,... are prime factor of n. + +3 Euler's Property: +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. + +Applying this 3 property 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)). + +*/ + +ll phiFunction(ll n){ + ll res = n; + for(ll i=2;i*i<=n;i++){ + if(n%i==0){ + while(n%i==0){ + n/=i; + } + res-=res/i; + } + } + if(n>1)res-=res/n; + return res; +} + +int main(){ + ll t; + cin>>t; + while(t--){ + ll n; + cin>>n; + cout< Date: Sun, 29 Mar 2020 17:26:45 +0530 Subject: [PATCH 2/6] modifed the spaces and code structure --- math/eulers_totient_function.cpp | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/math/eulers_totient_function.cpp b/math/eulers_totient_function.cpp index 1d1d75edf..5b0f7c869 100644 --- a/math/eulers_totient_function.cpp +++ b/math/eulers_totient_function.cpp @@ -1,7 +1,6 @@ #include using namespace std; -typedef long long int ll; /** @@ -23,26 +22,22 @@ Hence Implementation in O(sqrt(n)). */ -ll phiFunction(ll n){ - ll res = n; - for(ll i=2;i*i<=n;i++){ - if(n%i==0){ - while(n%i==0){ - n/=i; +int phiFunction (int n ) { + int result = n ; + for ( ll i=2 ; i*i <= n ; i++ ) { + if ( n%i == 0 ) { + while ( n%i == 0 ) { + n /= i ; } - res-=res/i; + result -= result/i ; } } - if(n>1)res-=res/n; - return res; + if ( n > 1 ) result -= result/n ; + return result ; } -int main(){ - ll t; - cin>>t; - while(t--){ - ll n; - cin>>n; - cout<> n ; + cout << phiFunction ( n ) << endl ; } From ea362365545154fbae1302fded6bf234e3c7810c Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Sun, 29 Mar 2020 17:31:34 +0530 Subject: [PATCH 3/6] modified the spaces before braces --- math/eulers_totient_function.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/math/eulers_totient_function.cpp b/math/eulers_totient_function.cpp index 5b0f7c869..63058063c 100644 --- a/math/eulers_totient_function.cpp +++ b/math/eulers_totient_function.cpp @@ -22,22 +22,22 @@ Hence Implementation in O(sqrt(n)). */ -int phiFunction (int n ) { - int result = n ; - for ( ll i=2 ; i*i <= n ; i++ ) { - if ( n%i == 0 ) { - while ( n%i == 0 ) { - n /= i ; +int phiFunction(int n) { + int result = n; + for (ll i=2; i*i <= n; i++) { + if (n%i == 0) { + while (n%i == 0) { + n /= i; } - result -= result/i ; + result -= result/i; } } - if ( n > 1 ) result -= result/n ; - return result ; + if (n > 1) result -= result/n; + return result; } int main() { - int n ; - cin >> n ; - cout << phiFunction ( n ) << endl ; + int n; + cin >> n; + cout << phiFunction(n) << endl; } From 022e2f02e7b01c4ac5e34be2fc38a3255f83b509 Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Sun, 29 Mar 2020 17:36:33 +0530 Subject: [PATCH 4/6] modified by removing namespace using-directive --- math/eulers_totient_function.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/math/eulers_totient_function.cpp b/math/eulers_totient_function.cpp index 63058063c..f05c7dab4 100644 --- a/math/eulers_totient_function.cpp +++ b/math/eulers_totient_function.cpp @@ -1,6 +1,5 @@ -#include - -using namespace std; +/// C++ Program to find Euler Totient Function +#include /** @@ -22,6 +21,7 @@ Hence Implementation in O(sqrt(n)). */ +/// Function to caculate euler totient function int phiFunction(int n) { int result = n; for (ll i=2; i*i <= n; i++) { @@ -38,6 +38,6 @@ int phiFunction(int n) { int main() { int n; - cin >> n; - cout << phiFunction(n) << endl; + std::cin >> n; + std::cout << phiFunction(n) << endl; } From 1d6492bf333dcd18a0e11805810a6da2b8e57f24 Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Sun, 29 Mar 2020 17:39:39 +0530 Subject: [PATCH 5/6] resolve compilation error --- math/eulers_totient_function.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/math/eulers_totient_function.cpp b/math/eulers_totient_function.cpp index f05c7dab4..8165f030e 100644 --- a/math/eulers_totient_function.cpp +++ b/math/eulers_totient_function.cpp @@ -24,7 +24,7 @@ Hence Implementation in O(sqrt(n)). /// Function to caculate euler totient function int phiFunction(int n) { int result = n; - for (ll i=2; i*i <= n; i++) { + for (int i=2; i*i <= n; i++) { if (n%i == 0) { while (n%i == 0) { n /= i; @@ -39,5 +39,5 @@ int phiFunction(int n) { int main() { int n; std::cin >> n; - std::cout << phiFunction(n) << endl; + std::cout << phiFunction(n); } From 58f6815aadf37b55f6d89bf298975f04a52aeddb Mon Sep 17 00:00:00 2001 From: John Law Date: Tue, 31 Mar 2020 23:26:32 +0200 Subject: [PATCH 6/6] Format Euler's Totient --- math/eulers_totient_function.cpp | 48 +++++++++++++++----------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/math/eulers_totient_function.cpp b/math/eulers_totient_function.cpp index 8165f030e..31ced5a51 100644 --- a/math/eulers_totient_function.cpp +++ b/math/eulers_totient_function.cpp @@ -1,38 +1,36 @@ /// C++ Program to find Euler Totient Function #include -/** +/* + * 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. + * 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 + */ -Euler Totient Function also know as phi function. - -phi(n) = phi(p1^a1).phi(p2^a2)... -where p1, p2,... are prime factor of n. - -3 Euler's Property: -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. - -Applying this 3 property 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)). - -*/ - -/// Function to caculate euler totient function +// Function to caculate Euler's totient phi int phiFunction(int n) { int result = n; - for (int i=2; i*i <= n; i++) { - if (n%i == 0) { - while (n%i == 0) { + for (int i = 2; i * i <= n; i++) { + if (n % i == 0) { + while (n % i == 0) { n /= i; } - result -= result/i; + result -= result / i; } } - if (n > 1) result -= result/n; + if (n > 1) result -= result / n; return result; }