Added algorithm to find gcd of two numbers

This commit is contained in:
2021-02-11 20:18:38 -05:00
parent 4f20f23744
commit 61f83228e2

View File

@@ -464,3 +464,18 @@ pub fn swapString(strng: String, first: i32, second: i32) -> String{
} }
return swappedString; return swappedString;
} }
//This function returns the gcd of two numbers
pub fn gcd(in1: i32, in2: i32) -> i32{
let mut num1 = in1;
let mut num2 = in2;
while((num1 != 0) && (num2 != 0)){
if(num1 > num2){
num1 %= num2;
}
else{
num2 %= num1;
}
}
return num1 | num2;
}