mirror of
https://bitbucket.org/Mattrixwv/rustclasses.git
synced 2025-12-06 18:34:00 -05:00
Added algorithm to find gcd of two numbers
This commit is contained in:
@@ -464,3 +464,18 @@ pub fn swapString(strng: String, first: i32, second: i32) -> String{
|
||||
}
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user