Skip to content

Commit

Permalink
extended_euclidean_gcd.py: Add Extended Euclidean GCD Algorithm
Browse files Browse the repository at this point in the history
This adds Extended Euclidean Algorithm with Python implementation of the same.
This implementation uses iterative method of the algorithm, takes two
values and gives out the gcd along with the polynomial equation to get
inverse directly if the gcd is 1. Code uses generic notations and
naming standard used in documentation along with that found in Wikipedia.

Closes NITSkmOS#3 NITSkmOS#159
  • Loading branch information
thekaleidoscope committed Oct 2, 2018
1 parent 43c82c4 commit 7bec1c1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 28 deletions.
55 changes: 27 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,45 @@
# Welcome to the NITSkmOS/Algorithms

[![Gitter](https://badges.gitter.im/NITSkmOS/algo.svg)](https://gitter.im/NITSkmOS/algo?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
[![NITSkmOS Algorithms](https://img.shields.io/badge/NITSkmOS-Algorithms-blue.svg)](https://github.com/NITSkmOS/Algorithms)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/29024673e94b48459809432cdd280e39)](https://app.codacy.com/app/NITSkmOS/Algorithms?utm_source=github.com&utm_medium=referral&utm_content=NITSkmOS/Algorithms&utm_campaign=Badge_Grade_Settings)
[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/NITSkmOS/Algorithms/blob/master/LICENSE)


This repository contains examples of various algorithms written on different programming languages...

## Implemented Algorithms

| Algorithm | C | CPP | Java | Python |
|:----------------------------------------------------------------------------------------------- |:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|
| [Euclidean GCD](https://en.wikipedia.org/wiki/Euclidean_algorithm) | [:octocat:](euclidean_gcd/C) | | | [:octocat:](euclidean_gcd/Python) |
| [QuickSort](https://en.wikipedia.org/wiki/Quicksort) | | | | [:octocat:](quicksort/Python) |
| [Merge Sort](https://en.wikipedia.org/wiki/Merge_sort) | | | | [:octocat:](merge_sort/Python) |
| [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [:octocat:](insertion_sort/C) | [:octocat:](insertion_sort/Cpp) | | [:octocat:](insertion_sort/Python) |
| [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) | | | | [:octocat:](counting_sort/Python) |
| [Radix Sort](https://en.wikipedia.org/wiki/Radix_sort) | | | | [:octocat:](radix_sort/Python) |
| [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | | [:octocat:](binary_search/Cpp) | | [:octocat:](binary_search/Python) |
| [Bubble Sort](https://en.wikipedia.org/wiki/Bubble_sort) | [:octocat:](bubble_sort/C) | [:octocat:](bubble_sort/Cpp) | | |
| [Shell Sort](https://en.wikipedia.org/wiki/Shellsort) | | | | [:octocat:](shell_sort/Python) |

| Algorithm | C | CPP | Java | Python |
| :------------------------------------------------------------------------------------ | :---------------------------: | :-----------------------------: | :--: | :----------------------------------------: |
| [Euclidean GCD](https://en.wikipedia.org/wiki/Euclidean_algorithm) | [:octocat:](euclidean_gcd/C) | | | [:octocat:](euclidean_gcd/Python) |
| [Extended Euclidean GCD](https://en.wikipedia.org/wiki/Extended_Euclidean_algorithmm) | | | | [:octocat:](extended_euclidean_gcd/Python) |
| [QuickSort](https://en.wikipedia.org/wiki/Quicksort) | | | | [:octocat:](quicksort/Python) |
| [Merge Sort](https://en.wikipedia.org/wiki/Merge_sort) | | | | [:octocat:](merge_sort/Python) |
| [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [:octocat:](insertion_sort/C) | [:octocat:](insertion_sort/Cpp) | | [:octocat:](insertion_sort/Python) |
| [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) | | | | [:octocat:](counting_sort/Python) |
| [Radix Sort](https://en.wikipedia.org/wiki/Radix_sort) | | | | [:octocat:](radix_sort/Python) |
| [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | | [:octocat:](binary_search/Cpp) | | [:octocat:](binary_search/Python) |
| [Bubble Sort](https://en.wikipedia.org/wiki/Bubble_sort) | [:octocat:](bubble_sort/C) | [:octocat:](bubble_sort/Cpp) | | |
| [Shell Sort](https://en.wikipedia.org/wiki/Shellsort) | | | | [:octocat:](shell_sort/Python) |

## Implemented Data Structures

| Data Structure | C | CPP | Java | Python |
|:----------------------------------------------------------------------------------------------- |:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|
| [Queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) | | [:octocat:](queue/Cpp) | | |
| [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) | | | [:octocat:](stack/Java) | [:octocat:](stack/Python) |
| [Linear Linked List](https://en.wikipedia.org/wiki/Linked_list) | [:octocat:](linked_list/C) | | | |
| [AVL Tree](https://en.wikipedia.org/wiki/AVL_tree) | [:octocat:](avl_tree/C) | | [:octocat:](avl_tree/Java) | |

| Data Structure | C | CPP | Java | Python |
| :------------------------------------------------------------------ | :------------------------: | :--------------------: | :------------------------: | :-----------------------: |
| [Queue](<https://en.wikipedia.org/wiki/Queue_(abstract_data_type)>) | | [:octocat:](queue/Cpp) | | |
| [Stack](<https://en.wikipedia.org/wiki/Stack_(abstract_data_type)>) | | | [:octocat:](stack/Java) | [:octocat:](stack/Python) |
| [Linear Linked List](https://en.wikipedia.org/wiki/Linked_list) | [:octocat:](linked_list/C) | | | |
| [AVL Tree](https://en.wikipedia.org/wiki/AVL_tree) | [:octocat:](avl_tree/C) | | [:octocat:](avl_tree/Java) | |

## Sample Run

| Language | Steps |
| --------------- | ---------------------------------------------------------------------- |
| JavaScript | <pre>node [filename.js]</pre> |
| Python | <pre>python [filename.py]</pre> |
| C | <pre>gcc [filename.c]<br>./a.out # unix<br>a.exe # windows</pre> |
| CPP | <pre>g++ [filename.cpp]<br>./a.out # unix<br>a.exe # windows</pre> |
| Java | <pre>javac [filename.java]<br>java [filename]</pre> |

| Language | Steps |
| ---------- | ------------------------------------------------------------------ |
| JavaScript | <pre>node [filename.js]</pre> |
| Python | <pre>python [filename.py]</pre> |
| C | <pre>gcc [filename.c]<br>./a.out # unix<br>a.exe # windows</pre> |
| CPP | <pre>g++ [filename.cpp]<br>./a.out # unix<br>a.exe # windows</pre> |
| Java | <pre>javac [filename.java]<br>java [filename]</pre> |

## Contributing

Expand All @@ -51,6 +49,7 @@ This repository contains examples of various algorithms written on different pro
4. Commit your changes: `git commit -am 'Add some feature'`.

Go through [Commit Messages guidelines](CONTRIBUTING.md#write-good-commit-messages)

5. Push to the branch: `git push origin my-new-feature`
6. Submit a pull request :smile:

Expand Down
29 changes: 29 additions & 0 deletions extended_euclidean_gcd/Python/extended_euclidean_gcd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Element style: gcd(a,b)-> a*x + b*y = r = gcd(a,b)

# Inverse of elements if gcd(a,b)==1:
# * a^-1 mod (b) = x mod(b)
# * b^-1 mod (a) = y mod(a)

# Naming is according to Bézout's identity matching to that on Wikipedia


def extended_euclidean_gcd(a, b):
x, y, u, v = 0, 1, 1, 0
while a != 0:
q, r = b//a, b % a
m, n = x-u*q, y-v*q
b, a, x, y, u, v = a, r, u, v, m, n

gcd = b
return gcd, x, y


def main():
a, b = 26, 15
gcd, x, y = extended_euclidean_gcd(a, b)
print("GCD of {} and {} is {}".format(a, b, gcd))
print("The Equation : {}*{} + {}*{} = {}".format(a, x, b, y, gcd))


if __name__ == '__main__':
main()

0 comments on commit 7bec1c1

Please sign in to comment.