Skip to content

add rust, haskell euclidean #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 17, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions chapters/fundamental_algorithms/euclidean_algorithm/euclidean.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ The Euclidean Algorithm is truly fundamental to many other algorithms throughout
*-----------------------------------------------------------------------------*/

#include <iostream>
#include <math.h>

// Euclidean algorithm with mod
int euclid_mod(int a, int b){
Expand Down Expand Up @@ -118,7 +117,6 @@ int main(){
### C
```c
#include <stdio.h>
#include <math.h>

int euclid_mod(int a, int b){

Expand Down Expand Up @@ -277,3 +275,59 @@ namespace Euclidean_Algorithm
}
}
```

### Haskell

```haskell
euclidSub :: Integer -> Integer -> Integer
euclidSub a b =
if a == b then
a
else if a < b then
euclidSub a (b - a)
else
euclidSub (a - b) b

euclidMod :: Integer -> Integer -> Integer
euclidMod a 0 = a
euclidMod a b = euclidMod b (a `mod` b)

main :: IO ()
main = do
let chk1 = euclidMod (64 * 67) (64 * 81)
chk2 = euclidSub (128 * 12) (128 * 77)
putStrLn (show chk1)
putStrLn (show chk2)
return ()
```

### Rust

```rust
fn euclid_sub(mut a: u64, mut b: u64) -> u64 {
while a != b {
if a < b {
b = b - a;
} else {
a = a - b;
}
}
a
}

fn euclid_rem(mut a: u64, mut b: u64) -> u64 {
while b != 0 {
let tmp = b;
b = a % b;
a = tmp;
}
a
}

fn main() {
let chk1 = euclid_rem(64 * 67, 64 * 81);
let chk2 = euclid_sub(128 * 12, 128 * 77);
println!("{}", chk1);
println!("{}", chk2);
}
```