Added gcd function

This commit is contained in:
2021-05-28 15:52:18 -04:00
parent 3adf0c22af
commit 0c3d7ee0c0

View File

@@ -619,3 +619,26 @@ export function findNumOccurrence(str: string, ch: string){
//Return the number of times the character appeared in the string
return num;
}
//This function returns the GCD of the two numbers sent to it
export function gcd(num1: number, num2: number){
while((num1 != 0) && (num2 != 0)){
if(num1 > num2){
num1 %= num2;
}
else{
num2 %= num1;
}
}
return num1 | num2;
}
export function gcdBig(num1: bigint, num2: bigint){
while((num1 != 0n) && (num2 != 0n)){
if(num1 > num2){
num1 %= num2;
}
else{
num2 %= num1;
}
}
return num1 | num2;
}