Here is an implementation of the Euclidean algorithm in TypeScript to find the greatest common divisor (GCD) of two numbers:
index.ts279 chars16 lines
The above function takes two parameters a
and b
, which are the two numbers for which the GCD is to be calculated. The implementation first ensures that a
is greater than or equal to b
, as the algorithm requires. It then finds the GCD using the Euclidean algorithm, which repeatedly divides the larger number by the smaller number and takes the remainder until the remainder is 0. The final value of the smaller number is the GCD.
You can call this function like so:
index.ts98 chars4 lines
This will output:
index.ts27 chars2 lines
gistlibby LogSnag