Files
freeCodeCamp/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/least-common-multiple.english.md
Josh Soref 004b99bf8f chore: fix typos in spelling (#38100)
* spelling: accidentally

* spelling: announce

* spelling: assembly

* spelling: avoid

* spelling: backend

* spelling: because

* spelling: claimed

* spelling: candidate

* spelling: certification

* spelling: certified

* spelling: challenge

* spelling: circular

* spelling: it isn't

* spelling: coins

* spelling: combination

* spelling: compliant

* spelling: containers

* spelling: concise

* spelling: deprecated

* spelling: development

* spelling: donor

* spelling: error

* spelling: everything

* spelling: exceed

* spelling: exist

* spelling: falsy

* spelling: faulty

* spelling: forward

* spelling: handle

* spelling: indicates

* spelling: initial

* spelling: integers

* spelling: issealed

* spelling: javascript

* spelling: length

* spelling: maximum

* spelling: minimum

* spelling: mutable

* spelling: notifier

* spelling: coordinate

* spelling: passport

* spelling: perform

* spelling: permuter

* spelling: placeholder

* spelling: progressively

* spelling: semantic

* spelling: submission

* spelling: submit

* spelling: translations

* spelling: turquoise

* spelling: visualization

* spelling: without

* spelling: registration

* spelling: representation
2020-02-08 23:59:10 +05:30

2.9 KiB
Raw Blame History

id, title, challengeType, forumTopicId
id title challengeType forumTopicId
5a23c84252665b21eecc7edf Least common multiple 5 302301

Description

The least common multiple of 12 and 18 is 36, because 12 is a factor (12 × 3 = 36), and 18 is a factor (18 × 2 = 36), and there is no positive integer less than 36 that has both factors. As a special case, if either m or n is zero, then the least common multiple is zero. One way to calculate the least common multiple is to iterate all the multiples of m, until you find one that is also a multiple of n. If you already have gcd for greatest common divisor, then this formula calculates lcm. ( \operatorname{lcm}(m, n) = \frac{|m \times n|}{\operatorname{gcd}(m, n)} )

Instructions

Compute the least common multiple of an array of integers. Given m and n, the least common multiple is the smallest positive integer that has both m and n as factors.

Tests

tests:
  - text: <code>LCM</code> should be a function.
    testString: assert(typeof LCM == 'function', '<code>LCM</code> should be a function.');
  - text: <code>LCM([2, 4, 8])</code> should return a number.
    testString: assert(typeof LCM([2, 4, 8]) == 'number', '<code>LCM([2, 4, 8])</code> should return a number.');
  - text: <code>LCM([2, 4, 8])</code> should return <code>8</code>.
    testString: assert.equal(LCM([2, 4, 8]), 8, '<code>LCM([2, 4, 8])</code> should return <code>8</code>.');
  - text: <code>LCM([4, 8, 12])</code> should return <code>24</code>.
    testString: assert.equal(LCM([4, 8, 12]), 24, '<code>LCM([4, 8, 12])</code> should return <code>24</code>.');
  - text: <code>LCM([3, 4, 5, 12, 40])</code> should return <code>120</code>.
    testString: assert.equal(LCM([3, 4, 5, 12, 40]), 120, '<code>LCM([3, 4, 5, 12, 40])</code> should return <code>120</code>.');
  - text: <code>LCM([11, 33, 90])</code> should return <code>990</code>.
    testString: assert.equal(LCM([11, 33, 90]), 990, '<code>LCM([11, 33, 90])</code> should return <code>990</code>.');
  - text: <code>LCM([-50, 25, -45, -18, 90, 447])</code> should return <code>67050</code>.
    testString: assert.equal(LCM([-50, 25, -45, -18, 90, 447]), 67050, '<code>LCM([-50, 25, -45, -18, 90, 447])</code> should return <code>67050</code>.');

Challenge Seed

function LCM(A) {
  // Good luck!
}

Solution

function LCM (A) {
    var n = A.length, a = Math.abs(A[0]);
    for (var i = 1; i < n; i++)
     { var b = Math.abs(A[i]), c = a;
       while (a && b){ a > b ? a %= b : b %= a; }
       a = Math.abs(c*A[i])/(a+b);
     }
    return a;
}