0% found this document useful (0 votes)
9 views

Euclids' Algorithm

Uploaded by

Dixxy Scott
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Euclids' Algorithm

Uploaded by

Dixxy Scott
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

~Euclids' algorithm~

An efficient method to find the GCD of two numbers.


-> Idea = Division.

TIME COMPLEXITY = O(Log(min(a,b)))

Updates the GCD of the algorithm GCD(b%a, a) with its recursive calling. (GCD
extended)

Scales well for large integers thats why often preferred in cryptography and other
scientific or other calculations. It dosen't works with non integer values.

if GCD = 1, then both numbers are co prime.

APPLICATION:
-> Used in cryptography for GCD and modular inverses.
-> Number theory : Used for solving problems with divisibility and diophantine
equations.
-> Simplifying fractions : Used to simplify the numerator and denominator of a
fraction.

CODE:

import java.util.*;

class EuclidAlg{
static int GCD(int a, int b){
if(a == 0){
return b;
}
return(GCD(b%a, a));
}

public static void main(String[] args){


Scanner input = new Scanner(System.in);

int x = input.nextInt();
int y = input.nextInt();

System.out.printf("GCD = %d", GCD(x, y));


}
}

You might also like