Files
freeCodeCamp/curriculum/challenges/japanese/10-coding-interview-prep/project-euler/problem-47-distinct-primes-factors.md
2022-01-20 20:30:18 +01:00

2.4 KiB
Raw Permalink Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
5900f39c1000cf542c50feae 問題 47: 相異なる素因数 5 302145 problem-47-distinct-primes-factors

--description--

相異なる 2 つの素因数を持つ 2 つの連続する数のうち、最小のものは次のとおりです。

14 = 2 × 7
15 = 3 × 5

相異なる 3 つの素因数を持つ 3 つの連続する数のうち、最小のものは次のとおりです。

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

それぞれ 4 つの相異なる素因数を持つ 4 つの連続する整数のうち、最小のものを求めなさい。 それらの数の 1 つ目は何ですか。

--hints--

distinctPrimeFactors(2, 2) は数値を返します。

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

distinctPrimeFactors(2, 2) は 14 を返す必要があります。

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

distinctPrimeFactors(3, 3) は 644 を返す必要があります。

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

distinctPrimeFactors(4, 4) は 134043 を返す必要があります。

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

--seed--

--seed-contents--

function distinctPrimeFactors(targetNumPrimes, targetConsecutive) {

  return true;
}

distinctPrimeFactors(4, 4);

--solutions--

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;
}