Created
August 21, 2024 09:58
-
-
Save alishahlakhani/64827d7964f34c2641a2b2cd47a02707 to your computer and use it in GitHub Desktop.
Algorithm // Finding LCM in Typescript using Euclidean algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function findGDC(num1: number, num2: number): number { | |
| function calcMod(v1: number, v2: number): number { | |
| return v1 % v2; | |
| } | |
| let a = num1; | |
| let b = num2; | |
| while (b > 0) { | |
| const mod = calcMod(a, b); | |
| if (mod === 0) break; | |
| a = b; | |
| b = mod; | |
| } | |
| return b; | |
| } | |
| function findLCM(num1: number, num2: number): number { | |
| const gcd = findGDC(num1, num2); | |
| return (num1 * num2) / gcd; | |
| } | |
| const num1 = 692; | |
| const num2 = 908; | |
| console.log("LCM =", findLCM(num1, num2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment