From 0c3d7ee0c048dd5a53636bcfc271147d54fc6432 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Fri, 28 May 2021 15:52:18 -0400 Subject: [PATCH] Added gcd function --- Algorithms.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Algorithms.ts b/Algorithms.ts index 39e88d2..97838d5 100644 --- a/Algorithms.ts +++ b/Algorithms.ts @@ -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; +}