Skip to content

Fixing the euclid alg to allow for negative inputs #14

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 18, 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
14 changes: 14 additions & 0 deletions chapters/fundamental_algorithms/euclidean_algorithm/euclidean.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@ The Euclidean Algorithm is truly fundamental to many other algorithms throughout
*-----------------------------------------------------------------------------*/

#include <iostream>
#include <cmath>

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

int temp;
while (b != 0){
Expand All @@ -90,6 +93,8 @@ int euclid_mod(int a, int b){

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

while (a != b){
if (a > b){
Expand Down Expand Up @@ -117,8 +122,11 @@ int main(){
### C
```c
#include <stdio.h>
#include <stdlib.h>

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

int temp;
while (b != 0){
Expand All @@ -131,6 +139,8 @@ int euclid_mod(int a, int b){
}

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

while (a != b){
if (a > b){
Expand Down Expand Up @@ -163,6 +173,8 @@ int main(){
<body>
<script>
function euclid_mod(a, b){
a = Math.abs(a);
b = Math.abs(b);

var temp;
while (b != 0){
Expand All @@ -175,6 +187,8 @@ function euclid_mod(a, b){
}

function euclid_sub(a, b){
a = Math.abs(a);
b = Math.abs(b);

while (a != b){
if (a > b){
Expand Down