From 36e356e6fd599c05b95e0c4120ddb260ee99070c Mon Sep 17 00:00:00 2001 From: Nitin Chauhan <0nitinchauhan@gmail.com> Date: Thu, 14 Feb 2019 04:11:55 +0530 Subject: [PATCH] GCD function using Recursion in Python and Java (#25226) function in Python and method in Java to perform gcd using recursion --- .../index.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/guide/english/algorithms/greatest-common-divisor-euclidean/index.md b/guide/english/algorithms/greatest-common-divisor-euclidean/index.md index a81f133a99..ae268084df 100644 --- a/guide/english/algorithms/greatest-common-divisor-euclidean/index.md +++ b/guide/english/algorithms/greatest-common-divisor-euclidean/index.md @@ -65,6 +65,29 @@ function gcd(a, b) { } ``` +Python Code to Perform GCD using Recursion +```Python +def gcd(a, b): + if b == 0: + return a: + else: + return gcd(b, (a % b)) +``` + +Java Code to Perform GCD using Recursion +```Java +static int gcd(int a, int b) +{ + if(b == 0) + { + return a; + } + return gcd(b, a % b); +} + +``` + + You can also use the Euclidean Algorithm to find GCD of more than two numbers. Since, GCD is associative, the following operation is valid- `GCD(a,b,c) == GCD(GCD(a,b), c)`