

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The Euclidean algorithm is a way to find the greatest common divisor of two positive 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.
Typology: Lecture notes
1 / 3
This page cannot be seen from the preview
Don't miss anything!
Euclidean algorithms (Basic and Extended) The Euclidean algorithm is a way to find the greatest common divisor of two positive 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: The algorithm is based on the below facts. If we subtract a smaller number from a larger one (we reduce a larger number), GCD doesn’t change. So if we keep subtracting repeatedly the larger of two, we end up with GCD. Now instead of subtraction, if we divide the larger number, the algorithm stops when we find the remainder 0. Below is a recursive function to evaluate gcd using Euclid’s algorithm: // Java program to demonstrate Basic Euclidean Algorithm import java.lang.; import java.util.; class GFG { // extended Euclidean Algorithm public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); }
// Driver code public static void main(String[] args) { int a = 10, b = 15, g; // Function call g = gcd(a, b); System.out.println("GCD(" + a + " , " + b