Skip to content

Commit a413f08

Browse files
committed
Added the algorithms that I know of so far
1 parent 1a4657c commit a413f08

File tree

160 files changed

+4619
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

160 files changed

+4619
-0
lines changed

Arithmetic/euclid-gcd-extended.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Algorithm: Extended Euclidean Algorithm
2+
// Type: Number Theory
3+
// Time: O(log(min(a, b)))
4+
// Space: O(log(min(a, b)))
5+
6+
#include <stdio.h>
7+
8+
int extendedGCD(int a, int b, int* x, int* y) {
9+
if (b == 0) {
10+
*x = 1;
11+
*y = 0;
12+
return a;
13+
}
14+
int x1, y1;
15+
int gcd = extendedGCD(b, a % b, &x1, &y1);
16+
*x = y1;
17+
*y = x1 - (a / b) * y1;
18+
return gcd;
19+
}
20+
21+
// Example:
22+
// int main() {
23+
// int x, y;
24+
// int g = extendedGCD(30, 20, &x, &y);
25+
// printf("GCD: %d, x: %d, y: %d\n", g, x, y); // Output: GCD: 10, x: 1, y: -1
26+
// return 0;
27+
// }
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Algorithm: Extended Euclidean Algorithm
2+
// Type: Number Theory
3+
// Time: O(log(min(a, b)))
4+
// Space: O(log(min(a, b)))
5+
6+
public class ExtendedGCD {
7+
static class Result {
8+
int gcd, x, y;
9+
Result(int g, int x, int y) {
10+
this.gcd = g; this.x = x; this.y = y;
11+
}
12+
}
13+
14+
static Result extendedGCD(int a, int b) {
15+
if (b == 0)
16+
return new Result(a, 1, 0);
17+
Result r = extendedGCD(b, a % b);
18+
int x = r.y;
19+
int y = r.x - (a / b) * r.y;
20+
return new Result(r.gcd, x, y);
21+
}
22+
23+
public static void main(String[] args) {
24+
Result r = extendedGCD(30, 20);
25+
System.out.println("GCD: " + r.gcd + ", x: " + r.x + ", y: " + r.y);
26+
}
27+
}

Arithmetic/euclid-gcd-extended.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Algorithm: Extended Euclidean Algorithm
2+
// Type: Number Theory
3+
// Time: O(log(min(a, b)))
4+
// Space: O(log(min(a, b)))
5+
6+
function extendedGCD(a, b) {
7+
if (b === 0) return { gcd: a, x: 1, y: 0 };
8+
9+
const result = extendedGCD(b, a % b);
10+
const x = result.y;
11+
const y = result.x - Math.floor(a / b) * result.y;
12+
13+
return { gcd: result.gcd, x, y };
14+
}
15+
16+
// Example:
17+
// const res = extendedGCD(30, 20);
18+
// console.log(res); // { gcd: 10, x: 1, y: -1 }

Arithmetic/euclid-gcd-extended.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Algorithm: Extended Euclidean Algorithm
2+
# Type: Number Theory
3+
# Time: O(log(min(a, b)))
4+
# Space: O(log(min(a, b)))
5+
6+
def extended_gcd(a, b):
7+
if b == 0:
8+
return (a, 1, 0)
9+
gcd, x1, y1 = extended_gcd(b, a % b)
10+
x = y1
11+
y = x1 - (a // b) * y1
12+
return (gcd, x, y)
13+
14+
# Example: print(extended_gcd(30, 20)) # Output: (10, 1, -1)

Arithmetic/euclid-gcd-extended.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Algorithm: Extended Euclidean Algorithm
2+
// Type: Number Theory
3+
// Time Complexity: O(log(min(a, b)))
4+
// Space Complexity: O(1) iterative, O(log(min(a, b))) recursive
5+
6+
function extendedGCD(a, b):
7+
if b == 0:
8+
return (a, 1, 0)
9+
gcd, x1, y1 = extendedGCD(b, a % b)
10+
x = y1
11+
y = x1 - (a // b) * y1
12+
return (gcd, x, y)
13+

Arithmetic/euclid-gcd.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Algorithm: Euclidean Algorithm for GCD
2+
// Type: Number Theory
3+
// Time: O(log(min(a, b)))
4+
// Space: O(1)
5+
6+
#include <stdio.h>
7+
8+
int gcd(int a, int b) {
9+
while (b != 0) {
10+
int temp = b;
11+
b = a % b;
12+
a = temp;
13+
}
14+
return a;
15+
}
16+
17+
int main() {
18+
printf("%d\n", gcd(48, 18)); // Output: 6
19+
return 0;
20+
}

Arithmetic/euclid-gcd.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Algorithm: Euclidean Algorithm for GCD
2+
// Type: Number Theory
3+
// Time: O(log(min(a, b)))
4+
// Space: O(1)
5+
6+
public class GCD {
7+
static int gcd(int a, int b) {
8+
while (b != 0) {
9+
int temp = b;
10+
b = a % b;
11+
a = temp;
12+
}
13+
return a;
14+
}
15+
16+
public static void main(String[] args) {
17+
System.out.println(gcd(48, 18)); // Output: 6
18+
}
19+
}

Arithmetic/euclid-gcd.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Algorithm: Euclidean Algorithm for GCD
2+
// Type: Number Theory
3+
// Time: O(log(min(a, b)))
4+
// Space: O(1)
5+
6+
function gcd(a, b) {
7+
while (b !== 0) {
8+
[a, b] = [b, a % b];
9+
}
10+
return a;
11+
}
12+
13+
// Example: console.log(gcd(48, 18)); // Output: 6

Arithmetic/euclid-gcd.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Algorithm: Euclidean Algorithm for GCD
2+
# Type: Number Theory
3+
# Time: O(log(min(a, b)))
4+
# Space: O(1)
5+
6+
def gcd(a, b):
7+
while b != 0:
8+
a, b = b, a % b
9+
return a
10+
11+
# Example: print(gcd(48, 18)) # Output: 6

Arithmetic/euclid-gcd.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Algorithm: Euclidean Algorithm for GCD
2+
// Type: Number Theory, Recursive/Iterative
3+
// Time Complexity: O(log(min(a, b)))
4+
// Space Complexity: O(1) iterative, O(log(min(a, b))) recursive
5+
6+
function gcd(a, b):
7+
while b ≠ 0:
8+
temp = b
9+
b = a mod b
10+
a = temp
11+
return a

0 commit comments

Comments
 (0)