2018-09-30 22:01:58 +00:00
|
|
|
|
---
|
|
|
|
|
id: 5900f39c1000cf542c50feae
|
|
|
|
|
challengeType: 5
|
|
|
|
|
title: 'Problem 47: Distinct primes factors'
|
2019-08-05 16:17:33 +00:00
|
|
|
|
forumTopicId: 302145
|
2018-09-30 22:01:58 +00:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## 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
|
2018-10-04 13:37:37 +00:00
|
|
|
|
tests:
|
2018-10-20 18:02:47 +00:00
|
|
|
|
- text: <code>distinctPrimeFactors(2, 2)</code> should return 14.
|
2019-07-27 02:45:56 +00:00
|
|
|
|
testString: assert.strictEqual(distinctPrimeFactors(2, 2), 14);
|
2018-10-20 18:02:47 +00:00
|
|
|
|
- text: <code>distinctPrimeFactors(3, 3)</code> should return 644.
|
2019-07-27 02:45:56 +00:00
|
|
|
|
testString: assert.strictEqual(distinctPrimeFactors(3, 3), 644);
|
2018-10-20 18:02:47 +00:00
|
|
|
|
- text: <code>distinctPrimeFactors(4, 4)</code> should return 134043.
|
2019-07-27 02:45:56 +00:00
|
|
|
|
testString: assert.strictEqual(distinctPrimeFactors(4, 4), 134043);
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</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) {
|
2018-11-11 14:39:45 +00:00
|
|
|
|
function numberOfPrimeFactors(n) {
|
|
|
|
|
let factors = 0;
|
|
|
|
|
|
|
|
|
|
// Considering 2 as a special case
|
|
|
|
|
let firstFactor = true;
|
|
|
|
|
while (n % 2 == 0) {
|
|
|
|
|
n = n / 2;
|
|
|
|
|
if (firstFactor) {
|
|
|
|
|
factors++;
|
|
|
|
|
firstFactor = false;
|
2018-09-30 22:01:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-11 14:39:45 +00:00
|
|
|
|
// Adding other factors
|
|
|
|
|
for (let i = 3; i < Math.sqrt(n); i += 2) {
|
|
|
|
|
firstFactor = true;
|
|
|
|
|
while (n % i == 0) {
|
|
|
|
|
n = n / i;
|
|
|
|
|
if (firstFactor) {
|
|
|
|
|
factors++;
|
|
|
|
|
firstFactor = false;
|
2018-09-30 22:01:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-11 14:39:45 +00:00
|
|
|
|
|
|
|
|
|
if (n > 1) { factors++; }
|
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
return factors;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 14:39:45 +00:00
|
|
|
|
let number = 0;
|
|
|
|
|
let consecutive = 0;
|
|
|
|
|
|
|
|
|
|
while (consecutive < targetConsecutive) {
|
|
|
|
|
number++;
|
|
|
|
|
if (numberOfPrimeFactors(number) >= targetNumPrimes) {
|
|
|
|
|
consecutive++;
|
2018-09-30 22:01:58 +00:00
|
|
|
|
} else {
|
2018-11-11 14:39:45 +00:00
|
|
|
|
consecutive = 0;
|
2018-09-30 22:01:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-11 14:39:45 +00:00
|
|
|
|
return number - targetConsecutive + 1;
|
|
|
|
|
}
|
2018-09-30 22:01:58 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|