Skip to content

Commit 7ae5a39

Browse files
committed
Rust/Haskell/OCaml/C++ negative nums
also standardize C++ style
1 parent c9f1894 commit 7ae5a39

File tree

1 file changed

+57
-61
lines changed
  • chapters/fundamental_algorithms/euclidean_algorithm

1 file changed

+57
-61
lines changed

chapters/fundamental_algorithms/euclidean_algorithm/euclidean.md

Lines changed: 57 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -65,55 +65,42 @@ The Euclidean Algorithm is truly fundamental to many other algorithms throughout
6565
# Example Code
6666
### C++
6767
```cpp
68-
/*-------------euclidean.cpp--------------------------------------------------//
69-
*
70-
* Purpose: To implement euclidean algorithm to find the greatest common divisor
71-
*
72-
* Notes: Compile with g++ euclidean.cpp
73-
*
74-
*-----------------------------------------------------------------------------*/
75-
7668
#include <iostream>
7769
#include <cmath>
7870

79-
// Euclidean algorithm with mod
80-
int euclid_mod(int a, int b){
71+
// Euclidean algorithm using modulus
72+
int euclid_mod(int a, int b) {
8173
a = std::abs(a);
8274
b = std::abs(b);
83-
84-
int temp;
85-
while (b != 0){
86-
temp = b;
87-
b = a%b;
75+
while (b != 0) {
76+
auto temp = b;
77+
b = a % b;
8878
a = temp;
8979
}
9080

9181
return a;
9282
}
9383

9484
// Euclidean algorithm with subtraction
95-
int euclid_sub(int a, int b){
85+
int euclid_sub(int a, int b) {
9686
a = std::abs(a);
9787
b = std::abs(b);
98-
99-
while (a != b){
100-
if (a > b){
101-
a = a - b;
102-
}
103-
else{
104-
b = b - a;
88+
while (a != b) {
89+
if (a > b) {
90+
a -= b;
91+
} else {
92+
b -= a;
10593
}
10694
}
10795

10896
return a;
10997
}
11098

111-
int main(){
99+
int main() {
100+
auto check1 = euclid_mod(64*67, 64*81);
101+
auto check2 = euclid_sub(128*12, 128*77);
112102

113-
int check = euclid_mod(64*67, 64*81);
114-
int check2 = euclid_sub(128*12, 128*77);
115-
116-
std::cout << check << '\n';
103+
std::cout << check1 << '\n';
117104
std::cout << check2 << '\n';
118105
}
119106

@@ -122,7 +109,7 @@ int main(){
122109
### C
123110
```c
124111
#include <stdio.h>
125-
#include <stdlib.h>
112+
#include <math.h>
126113
127114
int euclid_mod(int a, int b){
128115
a = abs(a);
@@ -307,17 +294,19 @@ namespace Euclidean_Algorithm
307294

308295
```haskell
309296
euclidSub :: Integer -> Integer -> Integer
310-
euclidSub a b =
311-
if a == b then
312-
a
313-
else if a < b then
314-
euclidSub a (b - a)
315-
else
316-
euclidSub (a - b) b
297+
euclidSub a b = inner (abs a) (abs b) where
298+
inner a b =
299+
if a == b then
300+
a
301+
else if a < b then
302+
euclidSub a (b - a)
303+
else
304+
euclidSub (a - b) b
317305

318306
euclidMod :: Integer -> Integer -> Integer
319-
euclidMod a 0 = a
320-
euclidMod a b = euclidMod b (a `mod` b)
307+
euclidMod a b = inner (abs a) (abs b) where
308+
inner a 0 = a
309+
inner a b = inner b (a `mod` b)
321310

322311
main :: IO ()
323312
main = do
@@ -331,18 +320,22 @@ main = do
331320
### Rust
332321

333322
```rust
334-
fn euclid_sub(mut a: u64, mut b: u64) -> u64 {
323+
fn euclid_sub(mut a: i64, mut b: i64) -> i64 {
324+
a = a.abs();
325+
b = b.abs();
335326
while a != b {
336327
if a < b {
337-
b = b - a;
328+
b -= a;
338329
} else {
339-
a = a - b;
330+
a -= b;
340331
}
341332
}
342333
a
343334
}
344335

345-
fn euclid_rem(mut a: u64, mut b: u64) -> u64 {
336+
fn euclid_rem(mut a: i64, mut b: i64) -> i64 {
337+
a = a.abs();
338+
b = b.abs();
346339
while b != 0 {
347340
let tmp = b;
348341
b = a % b;
@@ -362,24 +355,27 @@ fn main() {
362355
### OCaml
363356

364357
```ocaml
365-
let rec euclid_mod a b =
366-
if b = 0 then
367-
a
368-
else
369-
euclid_mod b (a mod b)
370-
371-
let rec euclid_sub a b =
372-
if a = b then
373-
a
374-
else if a < b then
375-
euclid_sub a (b - a)
376-
else
377-
euclid_sub (a - b) b
358+
let euclid_mod a b =
359+
let rec inner a = function
360+
| 0 -> a
361+
| b -> inner b (a mod b)
362+
in (inner (abs a) (abs b))
363+
364+
let euclid_sub a b =
365+
let rec inner a b =
366+
if a = b then
367+
a
368+
else if a < b then
369+
inner a (b - a)
370+
else
371+
inner (a - b) b
372+
in (inner (abs a) (abs b))
378373
379374
let chk1 = euclid_mod (64 * 67) (64 * 81)
380375
let chk2 = euclid_sub (128 * 12) (128 * 77)
381-
let () = print_string ((int_of_string chk1) ^ "\n")
382-
let () = print_string ((int_of_string chk2) ^ "\n")
376+
let () =
377+
chk1 |> print_int |> print_newline;
378+
chk2 |> print_int |> print_newline
383379
```
384380

385381
### Java
@@ -393,8 +389,8 @@ public static void main(String[] args) {
393389
}
394390

395391
public static int euclidSub(int a, int b) {
396-
a = Math.abs(a);
397-
b = Math.abs(b);
392+
a = Math.abs(a);
393+
b = Math.abs(b);
398394

399395
while (a != b) {
400396
if (a > b) {
@@ -408,8 +404,8 @@ public static int euclidSub(int a, int b) {
408404
}
409405

410406
public static int euclidMod(int a, int b) {
411-
a = Math.abs(a);
412-
b = Math.abs(b);
407+
a = Math.abs(a);
408+
b = Math.abs(b);
413409

414410
while (b != 0){
415411
int temp = b;

0 commit comments

Comments
 (0)