| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | id: 5900f39e1000cf542c50feb1 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | title: 'Problem 50: Consecutive prime sum' | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | challengeType: 5 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | forumTopicId: 302161 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | dashedName: problem-50-consecutive-prime-sum | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --description--
 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | The prime 41, can be written as the sum of six consecutive primes: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <div style='text-align: center;'>41 = 2 + 3 + 5 + 7 + 11 + 13</div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This is the longest sum of consecutive primes that adds to a prime below one-hundred. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Which prime, below one-million, can be written as the sum of the most consecutive primes? | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --hints--
 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | `consecutivePrimeSum(1000)` should return a number. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert(typeof consecutivePrimeSum(1000) === 'number'); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | `consecutivePrimeSum(1000)` should return 953. | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | assert.strictEqual(consecutivePrimeSum(1000), 953); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | `consecutivePrimeSum(1000000)` should return 997651. | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | assert.strictEqual(consecutivePrimeSum(1000000), 997651); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-08-13 17:24:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | # --seed--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## --seed-contents--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | function consecutivePrimeSum(limit) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | consecutivePrimeSum(1000000); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --solutions--
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | ```js | 
					
						
							|  |  |  | function consecutivePrimeSum(limit) { | 
					
						
							|  |  |  |   function isPrime(num) { | 
					
						
							|  |  |  |     if (num < 2) { | 
					
						
							|  |  |  |       return false; | 
					
						
							|  |  |  |     } else if (num === 2) { | 
					
						
							|  |  |  |       return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     const sqrtOfNum = Math.floor(num ** 0.5); | 
					
						
							|  |  |  |     for (let i = 2; i <= sqrtOfNum + 1; i++) { | 
					
						
							|  |  |  |       if (num % i === 0) { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   function getPrimes(limit) { | 
					
						
							|  |  |  |     const primes = []; | 
					
						
							|  |  |  |     for (let i = 0; i <= limit; i++) { | 
					
						
							|  |  |  |       if (isPrime(i)) primes.push(i); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return primes; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const primes = getPrimes(limit); | 
					
						
							|  |  |  |   let primeSum = [...primes]; | 
					
						
							|  |  |  |   primeSum.reduce((acc, n, i) => { | 
					
						
							|  |  |  |     primeSum[i] += acc; | 
					
						
							|  |  |  |     return acc += n; | 
					
						
							|  |  |  |   }, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (let j = primeSum.length - 1; j >= 0; j--) { | 
					
						
							|  |  |  |     for (let i = 0; i < j; i++) { | 
					
						
							|  |  |  |       const sum = primeSum[j] - primeSum[i]; | 
					
						
							|  |  |  |       if (sum > limit) break; | 
					
						
							|  |  |  |       if (isPrime(sum) && primes.indexOf(sum) > -1) return sum; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ``` |