Skip to content

Commit 7ff5e7c

Browse files
committed
adding java implementation for euclidean algorithm
1 parent 940ebe0 commit 7ff5e7c

File tree

1 file changed

+33
-0
lines changed
  • chapters/fundamental_algorithms/euclidean_algorithm

1 file changed

+33
-0
lines changed

chapters/fundamental_algorithms/euclidean_algorithm/euclidean.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,36 @@ let chk2 = euclid_sub (128 * 12) (128 * 77)
354354
let () = print_string ((int_of_string chk1) ^ "\n")
355355
let () = print_string ((int_of_string chk2) ^ "\n")
356356
```
357+
358+
### Java
359+
```java
360+
// sumbitted by lolatomroflsinnlos
361+
public static void main(String[] args) {
362+
363+
System.out.println(euclidSub(64 * 67, 64 * 81));
364+
System.out.println(euclidMod(128 * 12, 128 * 77));
365+
366+
}
367+
368+
public static int euclidSub(int a, int b) {
369+
while (a != b) {
370+
if (a > b) {
371+
a -=b;
372+
} else {
373+
b -=a;
374+
}
375+
}
376+
377+
return a;
378+
}
379+
380+
public static int euclidMod(int a, int b) {
381+
while (b != 0){
382+
int temp = b;
383+
b = a % b;
384+
a = temp;
385+
}
386+
387+
return a;
388+
}
389+
```

0 commit comments

Comments
 (0)