From eba31375bd5c6407b0a1ddc41259e4aa49365884 Mon Sep 17 00:00:00 2001 From: Daredevil1343 <91751264+Daredevil1343@users.noreply.github.com> Date: Sun, 10 Oct 2021 12:57:50 +0530 Subject: [PATCH 1/2] Added Euclidean Algorithm and it's Application. Hi , I have added the famous Euclidean Algorithm to find GCD of numbers and also showed it's application by solving a CodeForces Division 2 (Round 81) Problem number D (Same GCDs) . Please Merge my PR under Hacktoberfest2021 tag , It'll be of huge help :) --- C++/Algorithm/Euclidean.cpp | 51 +++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 C++/Algorithm/Euclidean.cpp diff --git a/C++/Algorithm/Euclidean.cpp b/C++/Algorithm/Euclidean.cpp new file mode 100644 index 0000000..1f7912e --- /dev/null +++ b/C++/Algorithm/Euclidean.cpp @@ -0,0 +1,51 @@ +/* +Author : Aman Agarwal +Date : 10/10/2021 +Description : Euclidean Algorithm and It's Application. + +The Euclidean algorithm, discussed below, +allows to find the greatest common divisor +of two numbers a and b in "O(logmin(a,b))". +The algorithm was first described in Euclid's +"Elements" (circa 300 BC), but it is possible +that the algorithm has even earlier origins. + +I used the same concept to solve CodeForces Round 81 D. Same GCDs +Overall Time Complexity : O(sqrt(n)) +*/ + +#include + +using namespace std; + +long long n, m, t, k = 0, a, l, p; + +int gcd(int a, int b) { + if (b == 0) { + return a; + } else { + return gcd(b, a % b); + } +} +int main() { + cin >> t; + while (t--) { + cin >> a >> m; + n = m / gcd(a, m); + k = n; + for (long long i = 2; i * i <= n; i++) { + if (n % i == 0) { + k /= i; + k *= (i - 1); + while (n % i == 0) { + n /= i; + } + } + } + if (n != 1) { + k -= k / n; + } + cout << k << endl; + } + return 0; +} \ No newline at end of file From fdac43c7fa586014c19d434b51d638b5d677845d Mon Sep 17 00:00:00 2001 From: Daredevil1343 <91751264+Daredevil1343@users.noreply.github.com> Date: Sun, 10 Oct 2021 12:59:38 +0530 Subject: [PATCH 2/2] Update Euclidean.cpp --- C++/Algorithm/Euclidean.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/C++/Algorithm/Euclidean.cpp b/C++/Algorithm/Euclidean.cpp index 1f7912e..54e5946 100644 --- a/C++/Algorithm/Euclidean.cpp +++ b/C++/Algorithm/Euclidean.cpp @@ -48,4 +48,4 @@ int main() { cout << k << endl; } return 0; -} \ No newline at end of file +}