Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Euclidean algorithms (Basic and Extended), Lecture notes of Mathematics

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

2023/2024

Uploaded on 12/30/2024

hasib-shaikh
hasib-shaikh 🇮🇳

2 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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);
}
pf3

Partial preview of the text

Download Euclidean algorithms (Basic and Extended) and more Lecture notes Mathematics in PDF only on Docsity!

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

  • ") = " + g); a = 35; b = 10; g = gcd(a, b); System.out.println("GCD(" + a + " , " + b
  • ") = " + g); a = 31; b = 2; g = gcd(a, b); System.out.println("GCD(" + a + " , " + b
  • ") = " + g); } }

Output

GCD(10, 15) = 5

GCD(35, 10) = 5

GCD(31, 2) = 1