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

1.9 KiB
Raw Blame History

id title challengeType forumTopicId dashedName
5900f39c1000cf542c50feae Problem 47: Distinct primes factors 5 302145 problem-47-distinct-primes-factors

--description--

The first two consecutive numbers to have two distinct prime factors are:

14 = 2 × 7
15 = 3 × 5

The first three consecutive numbers to have three distinct prime factors are:

644 = 22 × 7 × 23
645 = 3 × 5 × 43
646 = 2 × 17 × 19

Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?

--hints--

distinctPrimeFactors(2, 2) should return a number.

assert(typeof distinctPrimeFactors(2, 2) === 'number');

distinctPrimeFactors(2, 2) should return 14.

assert.strictEqual(distinctPrimeFactors(2, 2), 14);

distinctPrimeFactors(3, 3) should return 644.

assert.strictEqual(distinctPrimeFactors(3, 3), 644);

distinctPrimeFactors(4, 4) should return 134043.

assert.strictEqual(distinctPrimeFactors(4, 4), 134043);

--seed--

--seed-contents--

function distinctPrimeFactors(targetNumPrimes, targetConsecutive) {

  return true;
}

distinctPrimeFactors(4, 4);

--solutions--

// Initalize factor count with seive
const NUMFACTORS = Array(135000).fill(0);
(function initFactors(num) {
  for (let i = 2; i < num; i++)
    if (NUMFACTORS[i] === 0)
      for (let j = i; j < num; j += i)
        NUMFACTORS[j]++;
})(135000);

function distinctPrimeFactors(targetNumPrimes, targetConsecutive) {
  let numConsecutive = 0;
  let currNumber = 10;
  while (numConsecutive < targetConsecutive) {
    if (NUMFACTORS[currNumber] === targetNumPrimes)
      numConsecutive++;
    else
      numConsecutive = 0;
    currNumber++;
  }
  return currNumber - targetConsecutive;
}