2021-06-15 00:49:18 -07:00
|
|
|
|
---
|
|
|
|
|
id: 5900f39c1000cf542c50feae
|
2022-02-19 12:56:08 +05:30
|
|
|
|
title: 'Problema 47: Fattori primi distinti'
|
2021-06-15 00:49:18 -07:00
|
|
|
|
challengeType: 5
|
|
|
|
|
forumTopicId: 302145
|
|
|
|
|
dashedName: problem-47-distinct-primes-factors
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# --description--
|
|
|
|
|
|
2022-02-19 12:56:08 +05:30
|
|
|
|
I primi due numeri consecutivi ad avere due distinti fattori primi sono:
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
|
|
<div style='padding-left: 4em;'>
|
|
|
|
|
14 = 2 × 7<br>
|
|
|
|
|
15 = 3 × 5
|
|
|
|
|
</div>
|
|
|
|
|
|
2022-02-19 12:56:08 +05:30
|
|
|
|
I primi tre numeri consecutivi che hanno tre fattori primi distinti sono:
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
|
|
<div style='padding-left: 4em;'>
|
|
|
|
|
644 = 2<sup>2</sup> × 7 × 23<br>
|
|
|
|
|
645 = 3 × 5 × 43<br>
|
|
|
|
|
646 = 2 × 17 × 19
|
|
|
|
|
</div>
|
|
|
|
|
|
2022-02-19 12:56:08 +05:30
|
|
|
|
Trova i primi quattro interi consecutivi ad avere quattro distinti fattori primi ciascuno. Qual è il primo di questi numeri?
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
|
2022-02-19 12:56:08 +05:30
|
|
|
|
`distinctPrimeFactors(2, 2)` dovrebbe restituire un numero.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(typeof distinctPrimeFactors(2, 2) === 'number');
|
|
|
|
|
```
|
|
|
|
|
|
2022-02-19 12:56:08 +05:30
|
|
|
|
`distinctPrimeFactors(2, 2)` dovrebbe restituire 14.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(distinctPrimeFactors(2, 2), 14);
|
|
|
|
|
```
|
|
|
|
|
|
2022-02-19 12:56:08 +05:30
|
|
|
|
`distinctPrimeFactors(3, 3)` dovrebbe restituire 644.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(distinctPrimeFactors(3, 3), 644);
|
|
|
|
|
```
|
|
|
|
|
|
2022-02-19 12:56:08 +05:30
|
|
|
|
`distinctPrimeFactors(4, 4)` dovrebbe restituire 134043.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(distinctPrimeFactors(4, 4), 134043);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function distinctPrimeFactors(targetNumPrimes, targetConsecutive) {
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
distinctPrimeFactors(4, 4);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# --solutions--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
```
|