Skip to content

Rust/Haskell/OCaml/C++ negative nums #17

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 3 commits into from
Sep 19, 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
181 changes: 92 additions & 89 deletions chapters/fundamental_algorithms/euclidean_algorithm/euclidean.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,16 @@ The Euclidean Algorithm is truly fundamental to many other algorithms throughout
# Example Code
### C++
```cpp
/*-------------euclidean.cpp--------------------------------------------------//
*
* Purpose: To implement euclidean algorithm to find the greatest common divisor
*
* Notes: Compile with g++ euclidean.cpp
*
*-----------------------------------------------------------------------------*/

#include <iostream>
#include <cmath>

// Euclidean algorithm with mod
int euclid_mod(int a, int b){
// Euclidean algorithm using modulus
int euclid_mod(int a, int b)
{
a = std::abs(a);
b = std::abs(b);

int temp;
while (b != 0){
temp = b;
while (b != 0) {
int temp = b;
b = a%b;
a = temp;
}
Expand All @@ -92,28 +83,28 @@ int euclid_mod(int a, int b){
}

// Euclidean algorithm with subtraction
int euclid_sub(int a, int b){
int euclid_sub(int a, int b)
{
a = std::abs(a);
b = std::abs(b);

while (a != b){
if (a > b){
a = a - b;
while (a != b) {
if (a > b) {
a -= b;
}
else{
b = b - a;
else {
b -= a;
}
}

return a;
}

int main(){

int check = euclid_mod(64*67, 64*81);
int check2 = euclid_sub(128*12, 128*77);
int main()
{
auto check1 = euclid_mod(64*67, 64*81);
auto check2 = euclid_sub(128*12, 128*77);

std::cout << check << '\n';
std::cout << check1 << '\n';
std::cout << check2 << '\n';
}

Expand All @@ -122,44 +113,45 @@ int main(){
### C
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int euclid_mod(int a, int b){
int euclid_mod(int a, int b)
{
a = abs(a);
b = abs(b);

int temp;
while (b != 0){
temp = b;
int temp = b;
b = a%b;
a = temp;
}

return a;
}

int euclid_sub(int a, int b){
int euclid_sub(int a, int b)
{
a = abs(a);
b = abs(b);

while (a != b){
if (a > b){
a = a - b;
while (a != b) {
if (a > b) {
a -= b;
}
else{
b = b - a;
else {
b -= a;
}
}

return a;
}

int main(){

int check = euclid_mod(64*67, 64*81);
int main()
{
int check1 = euclid_mod(64*67, 64*81);
int check2 = euclid_sub(128*12, 128*77);

printf("%d\n", check);
printf("%d\n", check1);
printf("%d\n", check2);
}

Expand Down Expand Up @@ -248,7 +240,7 @@ print euclid_sub(128 * 12, 128 * 77)
### C#

```cs
// submitted by Julian Schacher
// submitted by Julian Schacher
using System;

namespace Euclidean_Algorithm
Expand Down Expand Up @@ -307,17 +299,19 @@ namespace Euclidean_Algorithm

```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
euclidSub a b = inner (abs a) (abs b) where
inner 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)
euclidMod a b = inner (abs a) (abs b) where
inner a 0 = a
inner a b = inner b (a `mod` b)

main :: IO ()
main = do
Expand All @@ -331,55 +325,64 @@ main = do
### 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;
fn euclid_sub(mut a: i64, mut b: i64) -> i64 {
a = a.abs();
b = b.abs();
while a != b {
if a < b {
b -= a;
} else {
a -= b;
}
}
}
a

a
}

fn euclid_rem(mut a: u64, mut b: u64) -> u64 {
while b != 0 {
let tmp = b;
b = a % b;
a = tmp;
}
a
fn euclid_rem(mut a: i64, mut b: i64) -> i64 {
a = a.abs();
b = b.abs();
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);
let chk1 = euclid_rem(64 * 67, 64 * 81);
let chk2 = euclid_sub(128 * 12, 128 * 77);
println!("{}", chk1);
println!("{}", chk2);
}
```

### OCaml

```ocaml
let rec euclid_mod a b =
if b = 0 then
a
else
euclid_mod b (a mod b)

let rec euclid_sub a b =
if a = b then
a
else if a < b then
euclid_sub a (b - a)
else
euclid_sub (a - b) b
let euclid_mod a b =
let rec inner a = function
| 0 -> a
| b -> inner b (a mod b)
in (inner (abs a) (abs b))

let euclid_sub a b =
let rec inner a b =
if a = b then
a
else if a < b then
inner a (b - a)
else
inner (a - b) b
in (inner (abs a) (abs b))

let chk1 = euclid_mod (64 * 67) (64 * 81)
let chk2 = euclid_sub (128 * 12) (128 * 77)
let () = print_string ((int_of_string chk1) ^ "\n")
let () = print_string ((int_of_string chk2) ^ "\n")
let () =
chk1 |> print_int |> print_newline;
chk2 |> print_int |> print_newline
```

### Java
Expand All @@ -393,8 +396,8 @@ public static void main(String[] args) {
}

public static int euclidSub(int a, int b) {
a = Math.abs(a);
b = Math.abs(b);
a = Math.abs(a);
b = Math.abs(b);

while (a != b) {
if (a > b) {
Expand All @@ -408,8 +411,8 @@ public static int euclidSub(int a, int b) {
}

public static int euclidMod(int a, int b) {
a = Math.abs(a);
b = Math.abs(b);
a = Math.abs(a);
b = Math.abs(b);

while (b != 0){
int temp = b;
Expand All @@ -418,5 +421,5 @@ public static int euclidMod(int a, int b) {
}

return a;
}
}
```