2018-09-30 22:01:58 +00:00
|
|
|
---
|
|
|
|
id: 5900f38f1000cf542c50fea2
|
|
|
|
title: 'Problem 35: Circular primes'
|
2020-11-27 18:02:05 +00:00
|
|
|
challengeType: 5
|
2019-08-05 16:17:33 +00:00
|
|
|
forumTopicId: 302009
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: problem-35-circular-primes
|
2018-09-30 22:01:58 +00:00
|
|
|
---
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
# --description--
|
2020-02-28 12:39:47 +00:00
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.
|
2020-02-28 12:39:47 +00:00
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
|
|
How many circular primes are there below `n`, whereas 100 ≤ `n` ≤ 1000000?
|
2020-11-27 18:02:05 +00:00
|
|
|
|
|
|
|
**Note:**
|
2020-02-28 12:39:47 +00:00
|
|
|
|
2020-02-08 18:29:10 +00:00
|
|
|
Circular primes individual rotation can exceed `n`.
|
2020-02-28 12:39:47 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
# --hints--
|
|
|
|
|
|
|
|
`circularPrimes(100)` should return a number.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
```js
|
|
|
|
assert(typeof circularPrimes(100) === 'number');
|
2018-09-30 22:01:58 +00:00
|
|
|
```
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
`circularPrimes(100)` should return 13.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
```js
|
|
|
|
assert(circularPrimes(100) == 13);
|
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
`circularPrimes(100000)` should return 43.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
```js
|
2020-11-27 18:02:05 +00:00
|
|
|
assert(circularPrimes(100000) == 43);
|
|
|
|
```
|
2020-09-15 16:57:40 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
`circularPrimes(250000)` should return 45.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
```js
|
|
|
|
assert(circularPrimes(250000) == 45);
|
|
|
|
```
|
|
|
|
|
|
|
|
`circularPrimes(500000)` should return 49.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(circularPrimes(500000) == 49);
|
|
|
|
```
|
|
|
|
|
|
|
|
`circularPrimes(750000)` should return 49.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(circularPrimes(750000) == 49);
|
|
|
|
```
|
|
|
|
|
|
|
|
`circularPrimes(1000000)` should return 55.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(circularPrimes(1000000) == 55);
|
2018-09-30 22:01:58 +00:00
|
|
|
```
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
# --seed--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
## --seed-contents--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
```js
|
|
|
|
function circularPrimes(n) {
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
return n;
|
|
|
|
}
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
circularPrimes(1000000);
|
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
# --solutions--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
```js
|
2018-11-10 07:19:16 +00:00
|
|
|
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;
|
2018-09-30 22:01:58 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-10 07:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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]) {
|
2020-02-08 18:29:10 +00:00
|
|
|
// If the rotated value is 0 then it isn't a circular prime, break the loop
|
2018-11-10 07:19:16 +00:00
|
|
|
tmp = 0;
|
2018-09-30 22:01:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-11-10 07:19:16 +00:00
|
|
|
tmp++;
|
|
|
|
primes[x] = 0;
|
2018-09-30 22:01:58 +00:00
|
|
|
}
|
2018-11-10 07:19:16 +00:00
|
|
|
count += tmp;
|
2018-09-30 22:01:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
2018-11-10 07:19:16 +00:00
|
|
|
}
|
2018-09-30 22:01:58 +00:00
|
|
|
```
|