mirror of
https://bitbucket.org/Mattrixwv/typescriptclasses.git
synced 2025-12-06 18:33:59 -05:00
Added gcd function
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user