From 06d70b2ce83bf5cbe2650cec8313c297ed475072 Mon Sep 17 00:00:00 2001 From: Akshat2806 Date: Fri, 2 Oct 2020 12:03:36 +0530 Subject: [PATCH] ETF Sieve implementation --- General/ETF_sieve.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 General/ETF_sieve.cpp diff --git a/General/ETF_sieve.cpp b/General/ETF_sieve.cpp new file mode 100644 index 0000000..73e2790 --- /dev/null +++ b/General/ETF_sieve.cpp @@ -0,0 +1,45 @@ +//Time complexity-O(loglog(n)) +#include +using namespace std; + +#define ll long long int + + +void ETF(int n) +{ + + ll phi[n+1]; + for (int i=1; i<=n; i++) + phi[i] = i; //initialize all array elements with indices + + + for (int p=2; p<=n; p++) + { + //if phi[p] ==p implies phi[p] is not calculated and it is prime + if (phi[p] == p) + { + //etf of prime p=p-1 + phi[p] = p-1; + + + for (int i = 2*p; i<=n; i += p) + { + //calculating from the formula + phi[i] = (phi[i]/p) * (p-1); + } + } + } + + // Printing phi values + for (int i=1; i<=n; i++) + cout << "Totient of " << i << " is " + << phi[i] << endl; +} + +// Driver program to test above function +int main() +{ + int n = 10; + ETF(n); + return 0; +}