forked from Asiatik/codezilla
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added maths folder and an algorithm (Issue # 48) (Asiatik#54)
* Added Euclidean Algorithm * Added Eucledian algorithm
- Loading branch information
1 parent
7a3efb1
commit 619be7d
Showing
5 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]){ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |