2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 599d0ba974141b0f508b37d5
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								title: Emirp primes
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								challengeType: 5
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 302253
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								dashedName: emirp-primes
							 
						 
					
						
							
								
									
										
										
										
											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 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								An emirp (**prime** spelled backwards) are primes that when reversed (in their decimal representation) are a different prime.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# --instructions--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Write a function that:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< ul >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < li > Shows the first < code > n< / code >  emirp numbers.< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < li > Shows the emirp numbers in a range.< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < li > Shows the number of emirps in a range.< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < li > Shows the < code > n< sup > th< / sup > < / code >  emirp number.< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / ul >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The function should accept two parameters. The first will receive `n`  or the range as an array. The second will receive a boolean, that specifies if the function returns the emirps 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 or a number.
							 
						 
					
						
							
								
									
										
										
										
											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 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`emirps`  should be a function. 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert(typeof emirps === 'function');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`emirps(20,true)`  should return `[13,17,31,37,71,73,79,97,107,113,149,157,167,179,199,311,337,347,359,389]`  
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(emirps(20, true), [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  13,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  17,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  31,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  37,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  71,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  73,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  79,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  97,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  107,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  113,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  149,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  157,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  167,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  179,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  199,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  311,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  337,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  347,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  359,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  389
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								]);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`emirps(1000)`  should return `70529`  
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(emirps(1000), 70529);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`emirps([7700,8000],true)`  should return `[7717,7757,7817,7841,7867,7879,7901,7927,7949,7951,7963]`  
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(emirps([7700, 8000], true), [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  7717,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  7757,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  7817,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  7841,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  7867,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  7879,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  7901,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  7927,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  7949,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  7951,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  7963
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`emirps([7700,8000],true)`  should return `11`  
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert.deepEqual(emirps([7700, 8000], false), 11);
							 
						 
					
						
							
								
									
										
										
										
											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 emirps(n) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --solutions--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function emirps(num, showEmirps)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const is_prime = function(n)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (!(n % 2) || !(n % 3)) return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    let p = 1;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    while (p * p <  n ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    { if (n % (p += 4) == 0 || n % (p += 2) == 0)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                            { return false; } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const is_emirp = function(n) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const r = parseInt(n.toString().split('').reverse().join(''));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return r != n & &  is_prime(n) & &  is_prime(r);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  let i,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    arr = [];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  if (typeof num === 'number') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    for (i = 0; arr.length <  num ;  i + + )  if  ( is_emirp ( i ) )  arr . push ( i ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // first x emirps
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (showEmirps) return arr;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // xth emirp
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return arr.pop();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  if (Array.isArray(num)) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    for (i = num[0]; i < = num[1]; i++) if (is_emirp(i)) arr.push(i);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // emirps between x .. y
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (showEmirps) return arr;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // number of emirps between x .. y
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return arr.length;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```