Skip to content

Commit

Permalink
Added maths folder and an algorithm (Issue # 48) (Asiatik#54)
Browse files Browse the repository at this point in the history
* Added Euclidean Algorithm

* Added Eucledian algorithm
  • Loading branch information
GnikDroy authored and tstreamDOTh committed Sep 30, 2018
1 parent 7a3efb1 commit 619be7d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Maths/Euclidean Algorithm/C++/GCD.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>

//Using template here to support both long and ints.
template <class integer>
integer gcd(integer a, integer b)
{
if (a == 0 || b == 0)
{
return a + b;
}
return gcd(b, b % a);
}

int main( int argv,char* argc[] )
{
std::cout << gcd (100,10) << std::endl;
std::cout << gcd (-100,10) << std::endl;
std::cout << gcd (3,1) << std::endl;
std::cout << gcd(3L,1L);
}
16 changes: 16 additions & 0 deletions Maths/Euclidean Algorithm/C/GCD.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "stdio.h"

//You should probably function overload to support longs as well.
int gcd(int a, int b)
{
if (a==0 || b==0)
{
return a + b;
}
return gcd(b, b % a);
}

int main(int argc, char* argv[])
{
printf("%d\n%d\n%d",gcd(100,10),gcd(100,-10),gcd(3,7));
}
12 changes: 12 additions & 0 deletions Maths/Euclidean Algorithm/Java/GCD.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class GCD{

public static int gcd(int a, int b){
if ( a==0 || b==0){
return a+b;
}
return gcd(b, b % a)
}

public static void main(String args[]){
}
}
9 changes: 9 additions & 0 deletions Maths/Euclidean Algorithm/Python/GCD.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#Using python3
def gcd(a, b):
if a == 0 or b == 0:
return a + b
return gcd(b, a % b)

if __name__=='__main__':
print (gcd(10,100))
print (gcd(9,145))
15 changes: 15 additions & 0 deletions Maths/Euclidean Algorithm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Euclidean Algorithm

[Wikipedia Entry](https://en.wikipedia.org/wiki/Euclidean_algorithm)

This is one of the fastest method for finding the greatest common divisior between two numbers.


## Pseudocode

function gcd(a, b)
while b ≠ 0
t := b;
b := a mod b;
a := t;
return a;

0 comments on commit 619be7d

Please sign in to comment.