Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Kristofer Koishigawa <scissorsneedfoodtoo@gmail.com> Co-authored-by: Beau Carnes <beaucarnes@gmail.com>
		
			
				
	
	
	
		
			2.5 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	
			2.5 KiB
		
	
	
	
	
	
	
	
id, challengeType, isHidden, title, forumTopicId
| id | challengeType | isHidden | title | forumTopicId | 
|---|---|---|---|---|
| 5900f39c1000cf542c50feae | 5 | false | Problem 47: Distinct primes factors | 302145 | 
Description
The first two consecutive numbers to have two distinct prime factors are:
  14 = 2 × 7
15 = 3 × 5
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
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?
Instructions
Tests
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);
Challenge Seed
function distinctPrimeFactors(targetNumPrimes, targetConsecutive) {
  // Good luck!
  return true;
}
distinctPrimeFactors(4, 4);
Solution
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;
}