2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Extensible prime generator
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 598ee8b91b410510ae82efef
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								challengeType: 5
							 
						 
					
						
							
								
									
										
										
										
											2019-08-05 09:17:33 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 302262
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Description
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'description' >  
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Write a generator of prime numbers, in order, that will automatically adjust to accommodate the generation of any reasonably high prime.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The generator should be able to:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< ul >  
						 
					
						
							
								
									
										
										
										
											2019-06-14 20:04:16 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  < li > Show the first < code > n< / code >  prime numbers< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < li > Show the prime numbers in a range< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < li > Show the number of primes in a range< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < li > Show the < code > n< sup > th< / sup > < / code >  prime number< / li > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / ul >  
						 
					
						
							
								
									
										
										
										
											2019-06-14 20:04:16 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The function should have two parameters. The first will receive < code > n< / code >  or the range as an array. The second will receive a boolean, that specifies if the function returns the prime numbers as an array or a single number(the number of primes in the range or the < code > n< sup > th< / sup > < / code >  prime). According to the parameters the function should return an array.
							 
						 
					
						
							
								
									
										
										
										
											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:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  text: < code > primeGenerator</ code >  is a function.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-26 05:24:52 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert(typeof primeGenerator === 'function');
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: < code > primeGenerator</ code >  is a function.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-26 05:24:52 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert.deepEqual(primeGenerator(20, true), [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71]);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: < code > primeGenerator</ code >  is a function.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-26 05:24:52 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert.deepEqual(primeGenerator([100, 150], true), [101, 103, 107, 109, 113, 127, 131, 137, 139, 149]);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: < code > primeGenerator</ code >  is a function.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-26 05:24:52 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert.equal(primeGenerator([7700, 8000], false), 30);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: < code > primeGenerator</ code >  is a function.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-26 05:24:52 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert.equal(primeGenerator(10000, false), 104729);
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Challenge Seed
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'challengeSeed' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< div  id = 'js-seed' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2019-03-02 17:42:56 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								function primeGenerator(num, showPrimes) {
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  // Good luck!
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Solution
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'solution' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// noprotect
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function primeGenerator(num, showPrimes) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  let i,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    arr = [];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  function isPrime(num) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // try primes < = 16
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (num < = 16) { return (
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      num == 2 || num == 3 || num == 5 || num == 7 || num == 11 || num == 13
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    ); }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // cull multiples of 2, 3, 5 or 7
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (num % 2 == 0 || num % 3 == 0 || num % 5 == 0 || num % 7 == 0)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      { return false; }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // cull square numbers ending in 1, 3, 7 or 9
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    for (let i = 10; i * i < = num; i += 10) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      if (num % (i + 1) == 0) return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      if (num % (i + 3) == 0) return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      if (num % (i + 7) == 0) return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      if (num % (i + 9) == 0) return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  if (typeof num === 'number') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    for (i = 0; arr.length <  num ;  i + + )  if  ( isPrime ( i ) )  arr . push ( i ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // first x primes
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (showPrimes) return arr;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // xth prime
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return arr.pop();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  if (Array.isArray(num)) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    for (i = num[0]; i < = num[1]; i++) if (isPrime(i)) arr.push(i);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // primes between x .. y
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (showPrimes) return arr;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // number of primes between x .. y
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return arr.length;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >