2018-10-10 18:03:03 -04:00
---
id: 5900f39c1000cf542c50feae
challengeType: 5
title: 'Problem 47: Distinct primes factors'
2019-08-28 16:26:13 +03:00
forumTopicId: 302145
2018-10-10 18:03:03 -04:00
localeTitle: 'Проблема 47: Определенные коэффициенты простых чисел'
---
## Description
2019-08-28 16:26:13 +03:00
< section id = 'description' >
Первые два последовательных номера имеют два разных основных фактора: < div style = "padding-left: 4em;" > 14 = 2 × 7 < / div > < div style = "padding-left: 4em;" > 15 = 3 × 5 < / div > Первые три последовательных номера имеют три различных основных фактора: < 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 > Найдите первые четыре последовательных целых числа, каждый из которых имеет четыре различных основных фактора. Каков первый из этих чисел?
< / section >
2018-10-10 18:03:03 -04:00
## Instructions
2019-08-28 16:26:13 +03:00
< section id = 'instructions' >
< / section >
2018-10-10 18:03:03 -04:00
## Tests
< section id = 'tests' >
```yml
tests:
2019-08-28 16:26:13 +03:00
- text: < code > distinctPrimeFactors(2, 2)</ code > should return 14.
testString: assert.strictEqual(distinctPrimeFactors(2, 2), 14);
- text: < code > distinctPrimeFactors(3, 3)</ code > should return 644.
testString: assert.strictEqual(distinctPrimeFactors(3, 3), 644);
- text: < code > distinctPrimeFactors(4, 4)</ code > should return 134043.
testString: assert.strictEqual(distinctPrimeFactors(4, 4), 134043);
2018-10-10 18:03:03 -04: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
2019-08-28 16:26:13 +03:00
function distinctPrimeFactors(targetNumPrimes, targetConsecutive) {
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;
}
}
// 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;
}
}
}
if (n > 1) { factors++; }
return factors;
}
let number = 0;
let consecutive = 0;
while (consecutive < targetConsecutive ) {
number++;
if (numberOfPrimeFactors(number) >= targetNumPrimes) {
consecutive++;
} else {
consecutive = 0;
}
}
return number - targetConsecutive + 1;
}
2018-10-10 18:03:03 -04:00
```
2019-08-28 16:26:13 +03:00
2018-10-10 18:03:03 -04:00
< / section >