avatarSamuele

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1711

Abstract

you calculate the greatest common divisor? I talked about it a few days ago <a href="https://blog.stranianelli.com/js-tips-03-greatest-common-divisor/">in another post</a>:</p><div id="a06c" class="link-block"> <a href="https://el3um4s.medium.com/how-to-find-the-greatest-common-divisor-in-javascript-c1333aa313db"> <div> <div> <h2>How to Find the Greatest Common Divisor in JavaScript</h2> <div><h3>An Overview of Different Methods to Calculate GCD in JavaScript</h3></div> <div><p>el3um4s.medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*Nixp771RBKnhGVF1vazSjA.jpeg)"></div> </div> </div> </a> </div><div id="7f06"><pre><span class="hljs-keyword">const</span> <span class="hljs-title function_">gcd</span> = (<span class="hljs-params">x, y</span>) => (y === <span class="hljs-number">0</span> ? x : <span class="hljs-title function_">gcd</span>(y, x % y));</pre></div><p id="0395">Therefore, the problem of calculating the least common multiple in JavaScript can be solved in just two lines:</p><div id="b0fa"><pre><span class="hljs-keyword">const</span> <span class="hljs-title function_">gcd</span> = (<span class="hljs-params">x, y</span>) => (y === <span class="hljs-number">0</span> ? x : <span class="hljs-title function_">gcd</span>(y, x % y));

<span class="hljs-keyword">const</span> <span class="hljs-title function_">lcm</span> = (<span class="hljs-params">...n</span>) => n.<span class="hljs-title function_">reduce</span>(<span class="hljs-function

Options

">(<span class="hljs-params">x, y</span>) =></span> (x * y) / <span class="hljs-title function_">gcd</span>(x, y));</pre></div><p id="1a5d">Thanks for reading! Stay tuned for more.</p><p id="9ef0"><b><i>Don’t miss my next article — sign up for my <a href="https://medium.com/subscribe/@el3um4s">Medium email list</a></i></b></p><div id="5744" class="link-block"> <a href="https://el3um4s.medium.com/membership"> <div> <div> <h2>Join Medium with my referral link — Samuele</h2> <div><h3>Read every story from Samuele (and thousands of other writers on Medium). Not a Medium member? Join here and a piece of…</h3></div> <div><p>el3um4s.medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*lXrRGWaRn4rGLgBJ)"></div> </div> </div> </a> </div><p id="a3d8"><i>More content at <a href="https://plainenglish.io/"><b>PlainEnglish.io</b></a>.</i></p><p id="cabd"><i>Sign up for our <a href="http://newsletter.plainenglish.io/"><b>free weekly newsletter</b></a>. Follow us on <a href="https://twitter.com/inPlainEngHQ"><b>Twitter</b></a></i>, <a href="https://www.linkedin.com/company/inplainenglish/"><b><i>LinkedIn</i></b></a><b><i>, <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw">YouTube</a>, and</i></b><i> <a href="https://discord.gg/GtDtUAvyhW"><b>Discord</b></a><b>.</b></i></p><p id="888e"><b><i>Looking to scale your software startup</i></b><i>? Check out <a href="https://circuit.ooo/?utm=publication-post-cta"><b>Circuit</b></a>.</i></p></article></body>

JavaScript Tips

How to Calculate the Least Common Multiple in JavaScript

An introduction to the LCM and how to implement it in JavaScript

The problem today is about calculating the least common multiple of a set of numbers. In mathematics, the least common multiple of two positive integers a and b, denoted by LCM(a,b), is the smallest natural number that is divisible by both a and b. But let's start with the definition of the problem:

The Problem: Least Common Multiple

link to the kata

Write a function that calculates the least common multiple of its arguments; each argument is assumed to be a non-negative integer. In the case that there are no arguments (or the provided array in compiled languages is empty), return 1. If any argument is 0, return 0.

The Solution

To solve the problem, I can follow the instructions on Wikipedia:

It is essentially a matter of using the greatest common divisor. But how do you calculate the greatest common divisor? I talked about it a few days ago in another post:

const gcd = (x, y) => (y === 0 ? x : gcd(y, x % y));

Therefore, the problem of calculating the least common multiple in JavaScript can be solved in just two lines:

const gcd = (x, y) => (y === 0 ? x : gcd(y, x % y));

const lcm = (...n) => n.reduce((x, y) => (x * y) / gcd(x, y));

Thanks for reading! Stay tuned for more.

Don’t miss my next article — sign up for my Medium email list

More content at PlainEnglish.io.

Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.

Looking to scale your software startup? Check out Circuit.

Programming
Science
Technology
JavaScript
Algorithms
Recommended from ReadMedium