feat(interview-prep): Converting and transferring Rosetta problems (#35487)
* feat(interview-prep): Converting and transferring Rosetta problems * feat(interview-prep): Fixing challenges * fix(challenges): Added code tags * Update knight's-tour.md Moved text to instructions section and fixed a small typo. * feat(interview-prep): Update description of Knight's tour Co-Authored-By: NitronR <bhanur05@gmail.com> * feat(interview-prep): Update description of Kaprekar numbers Co-Authored-By: NitronR <bhanur05@gmail.com> * feat(inteview-prep): Kaprekar numbers description Added <code> tags. * fix(interview-prep): Update description Kaprekar Numbers Co-Authored-By: NitronR <bhanur05@gmail.com> * fix(interview-prep): Added code tags Kaprekar numbers Co-Authored-By: NitronR <bhanur05@gmail.com>
This commit is contained in:
committed by
The Coding Aviator
parent
cab064a183
commit
79394d141f
@@ -0,0 +1,72 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7edf
|
||||
title: Least common multiple
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='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 <i>m</i> or <i>n</i> is zero, then the least common multiple is zero.
|
||||
One way to calculate the least common multiple is to iterate all the multiples of <i>m</i>, until you find one that is also a multiple of <i>n</i>.
|
||||
If you already have <i>gcd</i> for <a href="http://rosettacode.org/wiki/greatest common divisor">greatest common divisor</a>, then this formula calculates <i>lcm</i>.
|
||||
\( \operatorname{lcm}(m, n) = \frac{|m \times n|}{\operatorname{gcd}(m, n)} \)
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Compute the least common multiple of an array of intergers.
|
||||
Given <i>m</i> and <i>n</i>, the least common multiple is the smallest positive integer that has both <i>m</i> and <i>n</i> as factors.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
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>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function LCM(A) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
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;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
Reference in New Issue
Block a user