Create Euclidean algorithm.cpp

pull/958/head
Khushi Jha 2022-10-06 20:36:51 +05:30 committed by GitHub
parent 9b6d8e0b77
commit 4701bc2270
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,50 @@
/*The Euclidean Algorithm is a technique for quickly finding the GCD of two integers.
GCD of two numbers is the largest number that divides both of them. A simple way to find GCD is to factorize both numbers and multiply common prime factors.
Basic Euclidean Algorithm for GCD:
If we subtract a smaller number from a larger one (we reduce a larger number), GCD doesnt change. So if we keep subtracting repeatedly the larger of two, we end up with GCD.
Now instead of subtraction, if we divide the smaller number, the algorithm stops when we find the remainder 0.
If A = 0 then GCD(A,B)=B, since the GCD(0,B)=B, and we can stop.
If B = 0 then GCD(A,B)=A, since the GCD(A,0)=A, and we can stop.
Write A in quotient remainder form (A = BQ + R)
Find GCD(B,R) using the Euclidean Algorithm since GCD(A,B) = GCD(B,R)
Below is a recursive function to evaluate gcd using Euclids algorithm: */
#include <bits/stdc++.h>
using namespace std;
// Function to return gcd of a and b
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Driver Code
int main()
{
int a,b;
cout<<"Enter two numbers : ";
cin>>a>>b>>endl;
// Function call
cout << "GCD(" << a << ", " << b << ") = " << gcd(a, b)
<< endl;
=
return 0;
}
/*
Sample input-output
GCD(10, 15) = 5
GCD(35, 10) = 5
GCD(31, 2) = 1
*/
//Time Complexity: O(Log min(a, b))
//Auxiliary Space: O(Log (min(a,b))