From d5b80329b83ccaca33cee9ede35285645077e5c0 Mon Sep 17 00:00:00 2001 From: Himalaya Prakash <54323654+Himalaya001@users.noreply.github.com> Date: Tue, 5 Oct 2021 01:56:32 +0530 Subject: [PATCH] Create Extended_eucledian.java --- Java/Extended_eucledian.java | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Java/Extended_eucledian.java diff --git a/Java/Extended_eucledian.java b/Java/Extended_eucledian.java new file mode 100644 index 00000000..29ca0fd3 --- /dev/null +++ b/Java/Extended_eucledian.java @@ -0,0 +1,36 @@ +// Java program to demonstrate working of extended +// Euclidean Algorithm + +import java.util.*; +import java.lang.*; + +class GCD { + // extended Euclidean Algorithm + public static int gcdExtended(int a, int b, int x, int y) + { + // Base Case + if (a == 0) { + x = 0; + y = 1; + return b; + } + + int x1 = 1, y1 = 1; // To store results of recursive call + int gcd = gcdExtended(b % a, a, x1, y1); + + // Update x and y using results of recursive + // call + x = y1 - (b / a) * x1; + y = x1; + + return gcd; + } + + public static void main(String[] args) + { + int x = 1, y = 1; + int a = 35, b = 15; + int g = gcdExtended(a, b, x, y); + System.out.print("gcd(" + a + ", " + b + ") = " + g); + } +}