* fix(curriculum): tests quotes * fix(curriculum): fill seed-teardown * fix(curriculum): fix tests and remove unneeded seed-teardown
111 lines
2.6 KiB
Markdown
111 lines
2.6 KiB
Markdown
---
|
||
id: 5900f39c1000cf542c50feae
|
||
challengeType: 5
|
||
title: 'Problem 47: Distinct primes factors'
|
||
---
|
||
|
||
## Description
|
||
<section id='description'>
|
||
The first two consecutive numbers to have two distinct prime factors are:
|
||
<div style='padding-left: 4em;'>14 = 2 × 7</div>
|
||
<div style='padding-left: 4em;'>15 = 3 × 5</div>
|
||
The first three consecutive numbers to have three distinct prime factors are:
|
||
<div style='padding-left: 4em;'>644 = 2² × 7 × 23</div>
|
||
<div style='padding-left: 4em;'>645 = 3 × 5 × 43</div>
|
||
<div style='padding-left: 4em;'>646 = 2 × 17 × 19</div>
|
||
Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?
|
||
</section>
|
||
|
||
## Instructions
|
||
<section id='instructions'>
|
||
|
||
</section>
|
||
|
||
## Tests
|
||
<section id='tests'>
|
||
|
||
```yml
|
||
tests:
|
||
- text: <code>distinctPrimeFactors(2, 2)</code> should return 14.
|
||
testString: assert.strictEqual(distinctPrimeFactors(2, 2), 14, '<code>distinctPrimeFactors(2, 2)</code> should return 14.');
|
||
- text: <code>distinctPrimeFactors(3, 3)</code> should return 644.
|
||
testString: assert.strictEqual(distinctPrimeFactors(3, 3), 644, '<code>distinctPrimeFactors(3, 3)</code> should return 644.');
|
||
- text: <code>distinctPrimeFactors(4, 4)</code> should return 134043.
|
||
testString: assert.strictEqual(distinctPrimeFactors(4, 4), 134043, '<code>distinctPrimeFactors(4, 4)</code> should return 134043.');
|
||
|
||
```
|
||
|
||
</section>
|
||
|
||
## Challenge Seed
|
||
<section id='challengeSeed'>
|
||
|
||
<div id='js-seed'>
|
||
|
||
```js
|
||
function distinctPrimeFactors(targetNumPrimes, targetConsecutive) {
|
||
// Good luck!
|
||
return true;
|
||
}
|
||
|
||
distinctPrimeFactors(4, 4);
|
||
```
|
||
|
||
</div>
|
||
|
||
|
||
|
||
</section>
|
||
|
||
## Solution
|
||
<section id='solution'>
|
||
|
||
|
||
```js
|
||
function distinctPrimeFactors(targetNumPrimes, targetConsecutive) {
|
||
|
||
function isPrime(num) {
|
||
for (let i = 2, s = Math.sqrt(num); i <= s; i++) {
|
||
if (num % i === 0) {
|
||
return false;
|
||
}
|
||
}
|
||
return num !== 1;
|
||
}
|
||
|
||
function getPrimeFactors(num) {
|
||
const factors = [];
|
||
for (let i = 2; i <= Math.sqrt(num); i++) {
|
||
if (num % i === 0) {
|
||
// found a factor
|
||
if (isPrime(i)) {
|
||
factors.push(i);
|
||
}
|
||
if (isPrime(num / i) && i !== Math.sqrt(num)) {
|
||
factors.push(num / i);
|
||
}
|
||
}
|
||
}
|
||
return factors;
|
||
}
|
||
|
||
function findConsecutiveNumbers() {
|
||
let number = 0;
|
||
let consecutive = 0;
|
||
while (consecutive < targetConsecutive) {
|
||
number++;
|
||
if (getPrimeFactors(number).length >= targetNumPrimes) {
|
||
consecutive++;
|
||
} else {
|
||
consecutive = 0;
|
||
}
|
||
}
|
||
return (number - targetConsecutive) + 1;
|
||
}
|
||
|
||
return findConsecutiveNumbers();
|
||
}
|
||
```
|
||
|
||
</section>
|