From 8fe62d874bc42cf2ae3bbbe449f5c654b38cd933 Mon Sep 17 00:00:00 2001 From: sangamcse Date: Thu, 13 Sep 2018 19:01:29 +0530 Subject: [PATCH] euclidean_gcd: Add recursive euclidean --- Euclidean GCD/Python/euclidean_gcd.py | 20 --------------- README.md | 2 +- euclidean_gcd/Python/euclidean_gcd.py | 36 +++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 21 deletions(-) delete mode 100644 Euclidean GCD/Python/euclidean_gcd.py create mode 100644 euclidean_gcd/Python/euclidean_gcd.py diff --git a/Euclidean GCD/Python/euclidean_gcd.py b/Euclidean GCD/Python/euclidean_gcd.py deleted file mode 100644 index 93ad8411..00000000 --- a/Euclidean GCD/Python/euclidean_gcd.py +++ /dev/null @@ -1,20 +0,0 @@ -def euclidean_gcd(first, second): - """ - Calculates GCD of two numbers using the division-based Euclidean Algorithm - :param first: First number - :param second: Second number - """ - while(second): - first, second = second, first % second - return first - - -def main(): - first, second = map(int, input('Enter 2 integers: ').split()) - print('GCD of {} and {} is: {}'.format(first, - second, - euclidean_gcd(first, second))) - - -if __name__ == '__main__': - main() diff --git a/README.md b/README.md index 2d1bbc6b..406c29e8 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This repository contains examples of various algorithms written on different pro | Algorithm | C | CPP | Java | Python | |:----------------------------------------------------------------------------------------------- |:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|:-------------------------------------:| -| [Euclidean GCD](https://en.wikipedia.org/wiki/Euclidean_algorithm) | | | | [:octocat:](Euclidean%20GCD/Python) | +| [Euclidean GCD](https://en.wikipedia.org/wiki/Euclidean_algorithm) | | | | [:octocat:](euclidean_gcd/Python) | ## Implemented Data Structures diff --git a/euclidean_gcd/Python/euclidean_gcd.py b/euclidean_gcd/Python/euclidean_gcd.py new file mode 100644 index 00000000..d1f1e91e --- /dev/null +++ b/euclidean_gcd/Python/euclidean_gcd.py @@ -0,0 +1,36 @@ +def euclidean_gcd(first, second): + """ + Calculates GCD of two numbers using the division-based Euclidean Algorithm + :param first: First number + :param second: Second number + """ + while(second): + first, second = second, first % second + return first + + +def euclidean_gcd_recursive(first, second): + """ + Calculates GCD of two numbers using the recursive Euclidean Algorithm + :param first: First number + :param second: Second number + """ + if not second: + return first + return euclidean_gcd_recursive(second, first % second) + + +def main(): + first, second = map(int, input('Enter 2 integers: ').split()) + print('Division-based: GCD of {} and {} is: {}'.format(first, + second, + euclidean_gcd( + first, second))) + print('Recursive: GCD of {} and {} is: {}'.format(first, + second, + euclidean_gcd_recursive( + first, second))) + + +if __name__ == '__main__': + main()