Created
September 12, 2019 14:06
-
-
Save gkucmierz/d90ef1d39aa2393d6bc1c31e9b8f683f to your computer and use it in GitHub Desktop.
lcm, gcd
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
| // Smallest Common Multiple | |
| // Least common multiple | |
| // Greatest common divisor | |
| function gcd(a, b) { | |
| if (a < 0) a = -a; | |
| if (b < 0) b = -b; | |
| if (b > a) { | |
| [a, b] = [b, a]; | |
| } | |
| while (1) { | |
| if (b == 0) return a; | |
| a %= b; | |
| if (a == 0) return b; | |
| b %= a; | |
| } | |
| } | |
| function lcm(a, b) { | |
| return a * b / gcd(a, b); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment