| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  | --- | 
					
						
							|  |  |  |  | id: 5900f39c1000cf542c50feae | 
					
						
							|  |  |  |  | title: 'Problem 47: Distinct primes factors' | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  |  | challengeType: 5 | 
					
						
							| 
									
										
										
										
											2019-08-05 09:17:33 -07:00
										 |  |  |  | forumTopicId: 302145 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  |  | dashedName: problem-47-distinct-primes-factors | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  | --- | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  |  | # --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
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  |  | # --hints--
 | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  |  | `distinctPrimeFactors(2, 2)` should return a number. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ```js | 
					
						
							|  |  |  |  | assert(typeof distinctPrimeFactors(2, 2) === 'number'); | 
					
						
							|  |  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  |  | `distinctPrimeFactors(2, 2)` should return 14. | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  |  | ```js | 
					
						
							|  |  |  |  | assert.strictEqual(distinctPrimeFactors(2, 2), 14); | 
					
						
							|  |  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  |  | `distinctPrimeFactors(3, 3)` should return 644. | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  |  | ```js | 
					
						
							|  |  |  |  | assert.strictEqual(distinctPrimeFactors(3, 3), 644); | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  |  | `distinctPrimeFactors(4, 4)` should return 134043. | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  |  | ```js | 
					
						
							|  |  |  |  | assert.strictEqual(distinctPrimeFactors(4, 4), 134043); | 
					
						
							|  |  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  |  | # --seed--
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ## --seed-contents--
 | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | ```js | 
					
						
							|  |  |  |  | function distinctPrimeFactors(targetNumPrimes, targetConsecutive) { | 
					
						
							| 
									
										
										
										
											2020-09-15 09:57:40 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  |   return true; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | distinctPrimeFactors(4, 4); | 
					
						
							|  |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  |  | # --solutions--
 | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | ```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
										 |  |  |  | ``` |