
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Write a program to calculate the least common multiple of two numbers JavaScript
We are required to write a function that accepts two numbers and returns their least common multiple.
Least Common Multiple (LCM)
The least common multiple of two numbers a and b is the smallest positive integer that is divisible by both a and b.
For example − The LCM of 6 and 8 is 24 because 24 is the smallest positive integer that is divided by both 6 and 8.
Method to calculate LCM
One of the many ways of calculating LCM of two numbers a and b is by dividing the product of a and b by the greatest integer (also known as greatest common divisor or GCD) that divides both a and b.
In case of 6 and 8, their product is 48 and the greatest integer that divides them both is 2 so their LCM is −
(6*8)/2 = 24
Having these things clear, now let’s move to the coding part −
Example
const lcm = (a, b) => { let min = Math.min(a, b); while(min >= 2){ if(a % min === 0 && b % min === 0){ return (a*b)/min; }; min--; }; return (a*b); }; console.log(lcm(6, 8)); console.log(lcm(16, 18)); console.log(lcm(0, 8)); console.log(lcm(11, 28)); console.log(lcm(18, 34));
Understanding the code
Since the greatest integer that exactly divides both the numbers will still be smaller or equal to the smaller of the two numbers, we are calculating LCM for, we run a decrementing loop from the smaller number all the way down to 2.
If in our iterations we find any number that divides both the numbers we can guarantee that it’s the largest number that divides them both because we are in a decrementing loop, so we return right there with the LCM.
If we iterate through the complete it means that we didn’t found any such number and 1 is the only number that divides them both (in other terms the numbers are co-prime), so we simply return their product.
Output
The output in the console will be −
24 144 0 308 306
- Related Articles
- Function to calculate the least common multiple of two numbers in JavaScript
- Finding the least common multiple of a range of numbers in JavaScript?
- Haskell Program to calculate the Lowest Common Multiple
- C program to find Highest Common Factor (HCF) and Least Common Multiple (LCM)
- Calculating least common of a range JavaScript
- Java program to calculate the product of two numbers
- Smallest Common Multiple of an array of numbers in JavaScript
- PHP program to calculate the repeated subtraction of two numbers
- C++ Program for the Common Divisors of Two Numbers?
- C++ Program for Common Divisors of Two Numbers?
- Python Program for Common Divisors of Two Numbers
- Java Program for Common Divisors of Two Numbers
- Python program to check if two lists have at least one common element
- C# program to check if two lists have at-least one element common
- Program to count number of common divisors of two numbers in Python
