Files
freeCodeCamp/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-47-distinct-primes-factors.english.md
Kristofer Koishigawa 6cfd0fc503 fix: improve Project Euler descriptions, challenge seeds, and test cases (#38016)
* fix: improve Project Euler descriptions and test case

Improve formatting of Project Euler test descriptions. Also add poker hands array and new test case for problem 54

* feat: add typeof tests and gave functions proper names for first 100 challenges

* fix: continue fixing test descriptions and adding "before test" sections

* fix: address review comments

* fix: adjust grids in 18 and 67 and fix some text that reference files rather than the given arrays

* fix: implement bug fixes and improvements from review

* fix: remove console.log statements from seed and solution
2020-02-28 06:39:47 -06:00

121 lines
2.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
id: 5900f39c1000cf542c50feae
challengeType: 5
title: 'Problem 47: Distinct primes factors'
forumTopicId: 302145
---
## Description
<section id='description'>
The first two consecutive numbers to have two distinct prime factors are:
<div style='padding-left: 4em;'>
14 = 2 × 7<br>
15 = 3 × 5
</div>
The first three consecutive numbers to have three distinct prime factors are:
<div style='padding-left: 4em;'>
644 = 2<sup>2</sup> × 7 × 23<br>
645 = 3 × 5 × 43<br>
646 = 2 × 17 × 19
</div>
Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?
</section>
## Instructions
<section id='instructions'>
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: <code>distinctPrimeFactors(2, 2)</code> should return a number.
testString: assert(typeof distinctPrimeFactors(2, 2) === 'number');
- 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);
```
</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
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;
}
```
</section>