freeCodeCamp/curriculum/challenges/chinese/10-coding-interview-prep/project-euler/problem-47-distinct-primes-...

107 lines
1.9 KiB
Markdown
Raw Normal View History

---
id: 5900f39c1000cf542c50feae
title: 问题47不同的素数因素
challengeType: 5
videoUrl: ''
dashedName: problem-47-distinct-primes-factors
---
# --description--
前两个连续数字有两个不同的素数因子是:
14 = 2×7
15 = 3×5
前三个连续数字有三个不同的素因子:
644 =2²×7×23
645 = 3×5×43
646 = 2×17×19
找到前四个连续的整数,每个整数有四个不同的素数因子。这些数字中的第一个是什么?
# --hints--
`distinctPrimeFactors(2, 2)`应该返回14。
```js
assert.strictEqual(distinctPrimeFactors(2, 2), 14);
```
`distinctPrimeFactors(3, 3)`应该返回644。
```js
assert.strictEqual(distinctPrimeFactors(3, 3), 644);
```
`distinctPrimeFactors(4, 4)`应该返回134043。
```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;
}
```