| 
									
										
										
										
											2021-06-15 00:49:18 -07:00
										 |  |  | --- | 
					
						
							|  |  |  | id: 5900f38f1000cf542c50fea2 | 
					
						
							| 
									
										
										
										
											2021-08-20 00:00:51 -07:00
										 |  |  | title: 'Problema 35: Números primos circulares' | 
					
						
							| 
									
										
										
										
											2021-06-15 00:49:18 -07:00
										 |  |  | challengeType: 5 | 
					
						
							|  |  |  | forumTopicId: 302009 | 
					
						
							|  |  |  | dashedName: problem-35-circular-primes | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # --description--
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 08:32:04 -08:00
										 |  |  | O número 197 é chamado de primo circular porque todas as rotações dos algarismos: 197, 971 e 719 formam números primos. | 
					
						
							| 
									
										
										
										
											2021-06-15 00:49:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 00:00:51 -07:00
										 |  |  | Há treze números primos menores que 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79 e 97. | 
					
						
							| 
									
										
										
										
											2021-06-15 00:49:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 00:00:51 -07:00
										 |  |  | Quantos números primos circulares existem abaixo de `n`, onde 100 ≤ `n` ≤ 1000000? | 
					
						
							| 
									
										
										
										
											2021-06-15 00:49:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 00:00:51 -07:00
										 |  |  | **Observação:** | 
					
						
							| 
									
										
										
										
											2021-06-15 00:49:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 00:00:51 -07:00
										 |  |  | A rotação dos números primos circulares pode ser maior que `n`. | 
					
						
							| 
									
										
										
										
											2021-06-15 00:49:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | # --hints--
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 00:00:51 -07:00
										 |  |  | `circularPrimes(100)` deve retornar um número. | 
					
						
							| 
									
										
										
										
											2021-06-15 00:49:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert(typeof circularPrimes(100) === 'number'); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 00:00:51 -07:00
										 |  |  | `circularPrimes(100)` deve retornar 13. | 
					
						
							| 
									
										
										
										
											2021-06-15 00:49:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert(circularPrimes(100) == 13); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 00:00:51 -07:00
										 |  |  | `circularPrimes(100000)` deve retornar 43. | 
					
						
							| 
									
										
										
										
											2021-06-15 00:49:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert(circularPrimes(100000) == 43); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 00:00:51 -07:00
										 |  |  | `circularPrimes(250000)` deve retornar 45. | 
					
						
							| 
									
										
										
										
											2021-06-15 00:49:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert(circularPrimes(250000) == 45); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 00:00:51 -07:00
										 |  |  | `circularPrimes(500000)` deve retornar 49. | 
					
						
							| 
									
										
										
										
											2021-06-15 00:49:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert(circularPrimes(500000) == 49); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 00:00:51 -07:00
										 |  |  | `circularPrimes(750000)` deve retornar 49. | 
					
						
							| 
									
										
										
										
											2021-06-15 00:49:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert(circularPrimes(750000) == 49); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 00:00:51 -07:00
										 |  |  | `circularPrimes(1000000)` deve retornar 55. | 
					
						
							| 
									
										
										
										
											2021-06-15 00:49:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert(circularPrimes(1000000) == 55); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # --seed--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## --seed-contents--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | function circularPrimes(n) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return n; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | circularPrimes(1000000); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # --solutions--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | function rotate(n) { | 
					
						
							|  |  |  |   if (n.length == 1) return n; | 
					
						
							|  |  |  |   return n.slice(1) + n[0]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function circularPrimes(n) { | 
					
						
							|  |  |  |   // Nearest n < 10^k | 
					
						
							|  |  |  |   const bound = 10 ** Math.ceil(Math.log10(n)); | 
					
						
							|  |  |  |   const primes = [0, 0, 2]; | 
					
						
							|  |  |  |   let count = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Making primes array | 
					
						
							|  |  |  |   for (let i = 4; i <= bound; i += 2) { | 
					
						
							|  |  |  |     primes.push(i - 1); | 
					
						
							|  |  |  |     primes.push(0); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Getting upperbound | 
					
						
							|  |  |  |   const upperBound = Math.ceil(Math.sqrt(bound)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Setting other non-prime numbers to 0 | 
					
						
							|  |  |  |   for (let i = 3; i < upperBound; i += 2) { | 
					
						
							|  |  |  |     if (primes[i]) { | 
					
						
							|  |  |  |       for (let j = i * i; j < bound; j += i) { | 
					
						
							|  |  |  |         primes[j] = 0; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Iterating through the array | 
					
						
							|  |  |  |   for (let i = 2; i < n; i++) { | 
					
						
							|  |  |  |     if (primes[i]) { | 
					
						
							|  |  |  |       let curr = String(primes[i]); | 
					
						
							|  |  |  |       let tmp = 1; // tmp variable to hold the no of rotations | 
					
						
							|  |  |  |       for (let x = rotate(curr); x != curr; x = rotate(x)) { | 
					
						
							|  |  |  |         if (x > n && primes[x]) { | 
					
						
							|  |  |  |           continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (!primes[x]) { | 
					
						
							|  |  |  |           // If the rotated value is 0 then it isn't a circular prime, break the loop | 
					
						
							|  |  |  |           tmp = 0; | 
					
						
							|  |  |  |           break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         tmp++; | 
					
						
							|  |  |  |         primes[x] = 0; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       count += tmp; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return count; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ``` |