2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: 5900f39c1000cf542c50feae
|
2020-12-16 07:37:30 +00:00
|
|
|
|
title: 问题47:不同的素数因素
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 5
|
|
|
|
|
videoUrl: ''
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: problem-47-distinct-primes-factors
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
前两个连续数字有两个不同的素数因子是:
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
14 = 2×7
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
15 = 3×5
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
前三个连续数字有三个不同的素因子:
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
644 =2²×7×23
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
645 = 3×5×43
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
646 = 2×17×19
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
找到前四个连续的整数,每个整数有四个不同的素数因子。这些数字中的第一个是什么?
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
`distinctPrimeFactors(2, 2)`应该返回14。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(distinctPrimeFactors(2, 2), 14);
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
`distinctPrimeFactors(3, 3)`应该返回644。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(distinctPrimeFactors(3, 3), 644);
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
`distinctPrimeFactors(4, 4)`应该返回134043。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
assert.strictEqual(distinctPrimeFactors(4, 4), 134043);
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
2020-08-13 15:24:35 +00:00
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function distinctPrimeFactors(targetNumPrimes, targetConsecutive) {
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
distinctPrimeFactors(4, 4);
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```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;
|
|
|
|
|
}
|
|
|
|
|
```
|