2018-09-30 22:01:58 +00:00
|
|
|
|
---
|
|
|
|
|
id: 5900f39c1000cf542c50feae
|
|
|
|
|
title: 'Problem 47: Distinct primes factors'
|
2020-11-27 18:02:05 +00:00
|
|
|
|
challengeType: 5
|
2019-08-05 16:17:33 +00:00
|
|
|
|
forumTopicId: 302145
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: problem-47-distinct-primes-factors
|
2018-09-30 22:01:58 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --description--
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
The first two consecutive numbers to have two distinct prime factors are:
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
|
|
|
|
<div style='padding-left: 4em;'>
|
|
|
|
|
14 = 2 × 7<br>
|
|
|
|
|
15 = 3 × 5
|
|
|
|
|
</div>
|
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
The first three consecutive numbers to have three distinct prime factors are:
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
|
|
|
|
<div style='padding-left: 4em;'>
|
|
|
|
|
644 = 2<sup>2</sup> × 7 × 23<br>
|
|
|
|
|
645 = 3 × 5 × 43<br>
|
|
|
|
|
646 = 2 × 17 × 19
|
|
|
|
|
</div>
|
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --hints--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`distinctPrimeFactors(2, 2)` should return a number.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(typeof distinctPrimeFactors(2, 2) === 'number');
|
|
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`distinctPrimeFactors(2, 2)` should return 14.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(distinctPrimeFactors(2, 2), 14);
|
|
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`distinctPrimeFactors(3, 3)` should return 644.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(distinctPrimeFactors(3, 3), 644);
|
2018-09-30 22:01:58 +00:00
|
|
|
|
```
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
`distinctPrimeFactors(4, 4)` should return 134043.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(distinctPrimeFactors(4, 4), 134043);
|
|
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function distinctPrimeFactors(targetNumPrimes, targetConsecutive) {
|
2020-09-15 16:57:40 +00:00
|
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
distinctPrimeFactors(4, 4);
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
|
# --solutions--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
|
|
```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
|
|
|
|
```
|