Skip to content

Commit 2089452

Browse files
committed
Merge pull request #127 from fclvbfm934/gh-pages
Added Euclidean Algorithm to the Number Theory section
2 parents b85e77e + f39309b commit 2089452

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

algorithm/category.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"number_theory": {
1616
"name": "Number Theory",
1717
"list": {
18-
"seive_of_erathrones": "Seive of Erathrones"
18+
"seive_of_erathrones": "Seive of Erathrones",
19+
"euclidean_algorithm": "Euclidean Algorithm"
1920
}
2021
},
2122
"cryptography": {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
logger._print("Finding the greatest common divisor of " + a[0] + " and " + a[1]);
2+
3+
logger._print("Checking if first number is at most the second number");
4+
5+
if(a[0] > a[1]) {
6+
var tmp = a[0];
7+
a[0] = a[1];
8+
a[1] = tmp;
9+
logger._print("The first number is bigger than the second number. Switching the numbers.");
10+
tracer._setData(a)._wait();
11+
}
12+
13+
while(a[0] > 0) {
14+
logger._print(a[1] + " % " + a[0] + " = " + a[1]%a[0]);
15+
logger._print("Switching a[1] with a[1]%a[0]");
16+
a[1] %= a[0];
17+
tracer._notify(1, a[1])._wait();
18+
logger._print("Now switching the two values to keep a[0] < a[1]");
19+
var tmp = a[0];
20+
a[0] = a[1];
21+
a[1] = tmp;
22+
tracer._notify(0, a[0]);
23+
tracer._notify(1, a[1])._wait();
24+
tracer._denotify(0);
25+
tracer._denotify(1);
26+
}
27+
28+
logger._print("The greatest common divisor is " + a[1]);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var tracer = new Array1DTracer('Euclidean Algorithm');
2+
var a = [];
3+
a.push(465);
4+
a.push(255)
5+
tracer._setData(a);
6+
var logger = new LogTracer();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Euclidean Algorithm": "Finds the greatest common divisor of two positive integers. The Euclidean Algorithm uses the fact that gcd(m, n) = gcd(m, n % m).",
3+
"Complexity": {
4+
"time": "O(log(m)log(n))",
5+
"space": "O(1)"
6+
},
7+
"References": [
8+
"<a href='https://en.wikipedia.org/wiki/Euclidean_algorithm'>Wikipedia</a>"
9+
],
10+
"files": {
11+
"basic": "Euclidean Algorithm"
12+
}
13+
}

0 commit comments

Comments
 (0)