| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  | --- | 
					
						
							|  |  |  |  | id: 5900f39c1000cf542c50feae | 
					
						
							|  |  |  |  | challengeType: 5 | 
					
						
							| 
									
										
										
										
											2020-05-21 17:31:25 +02:00
										 |  |  |  | isHidden: false | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  | title: 'Problem 47: Distinct primes factors' | 
					
						
							| 
									
										
										
										
											2019-08-05 09:17:33 -07:00
										 |  |  |  | forumTopicId: 302145 | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  | --- | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ## Description
 | 
					
						
							|  |  |  |  | <section id='description'> | 
					
						
							| 
									
										
										
										
											2020-02-28 21:39:47 +09:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  | The first two consecutive numbers to have two distinct prime factors are: | 
					
						
							| 
									
										
										
										
											2020-02-28 21:39:47 +09:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | <div style='padding-left: 4em;'> | 
					
						
							|  |  |  |  |   14 = 2 × 7<br> | 
					
						
							|  |  |  |  |   15 = 3 × 5 | 
					
						
							|  |  |  |  | </div> | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  | The first three consecutive numbers to have three distinct prime factors are: | 
					
						
							| 
									
										
										
										
											2020-02-28 21:39:47 +09:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | <div style='padding-left: 4em;'> | 
					
						
							|  |  |  |  |   644 = 2<sup>2</sup> × 7 × 23<br> | 
					
						
							|  |  |  |  |   645 = 3 × 5 × 43<br> | 
					
						
							|  |  |  |  |   646 = 2 × 17 × 19 | 
					
						
							|  |  |  |  | </div> | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  | Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers? | 
					
						
							| 
									
										
										
										
											2020-02-28 21:39:47 +09:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  | </section> | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ## Instructions
 | 
					
						
							|  |  |  |  | <section id='instructions'> | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | </section> | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ## Tests
 | 
					
						
							|  |  |  |  | <section id='tests'> | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ```yml | 
					
						
							| 
									
										
										
										
											2018-10-04 14:37:37 +01:00
										 |  |  |  | tests: | 
					
						
							| 
									
										
										
										
											2020-02-28 21:39:47 +09:00
										 |  |  |  |   - text: <code>distinctPrimeFactors(2, 2)</code> should return a number. | 
					
						
							|  |  |  |  |     testString: assert(typeof distinctPrimeFactors(2, 2) === 'number'); | 
					
						
							| 
									
										
										
										
											2018-10-20 21:02:47 +03:00
										 |  |  |  |   - text: <code>distinctPrimeFactors(2, 2)</code> should return 14. | 
					
						
							| 
									
										
										
										
											2019-07-26 19:45:56 -07:00
										 |  |  |  |     testString: assert.strictEqual(distinctPrimeFactors(2, 2), 14); | 
					
						
							| 
									
										
										
										
											2018-10-20 21:02:47 +03:00
										 |  |  |  |   - text: <code>distinctPrimeFactors(3, 3)</code> should return 644. | 
					
						
							| 
									
										
										
										
											2019-07-26 19:45:56 -07:00
										 |  |  |  |     testString: assert.strictEqual(distinctPrimeFactors(3, 3), 644); | 
					
						
							| 
									
										
										
										
											2018-10-20 21:02:47 +03:00
										 |  |  |  |   - text: <code>distinctPrimeFactors(4, 4)</code> should return 134043. | 
					
						
							| 
									
										
										
										
											2019-07-26 19:45:56 -07:00
										 |  |  |  |     testString: assert.strictEqual(distinctPrimeFactors(4, 4), 134043); | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | </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) { | 
					
						
							| 
									
										
										
										
											2018-11-11 20:09:45 +05:30
										 |  |  |  |   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; | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  |       } | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-11-11 20:09:45 +05:30
										 |  |  |  |     // 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; | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  |         } | 
					
						
							|  |  |  |  |       } | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-11-11 20:09:45 +05:30
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (n > 1) { factors++; } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  |     return factors; | 
					
						
							|  |  |  |  |   } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-11 20:09:45 +05:30
										 |  |  |  |   let number = 0; | 
					
						
							|  |  |  |  |   let consecutive = 0; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |   while (consecutive < targetConsecutive) { | 
					
						
							|  |  |  |  |     number++; | 
					
						
							|  |  |  |  |     if (numberOfPrimeFactors(number) >= targetNumPrimes) { | 
					
						
							|  |  |  |  |       consecutive++; | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-11-11 20:09:45 +05:30
										 |  |  |  |       consecutive = 0; | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-11-11 20:09:45 +05:30
										 |  |  |  |   return number - targetConsecutive + 1; | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | </section> |