fix(curriculum): Problem 35 - update solution
This commit is contained in:
@ -9,6 +9,9 @@ title: 'Problem 35: Circular primes'
|
|||||||
The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.
|
The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.
|
||||||
There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.
|
There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.
|
||||||
How many circular primes are there below n, whereas 100 <= n <= 1000000?
|
How many circular primes are there below n, whereas 100 <= n <= 1000000?
|
||||||
|
|
||||||
|
<br><strong>Note:</strong><br>
|
||||||
|
Circular primes individual rotation can exceeed `n`.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
@ -63,37 +66,57 @@ circularPrimes(1000000);
|
|||||||
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const circularPrimes = (n) => {
|
function rotate(n) {
|
||||||
const primeCheck = (num) => {
|
if (n.length == 1) return n;
|
||||||
if (num === 1) {
|
return n.slice(1) + n[0];
|
||||||
return false;
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
for (let i = 2; i <= Math.floor(Math.sqrt(num)); i++) {
|
|
||||||
if (num % i === 0) {
|
// Getting upperbound
|
||||||
return false;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
}
|
||||||
};
|
|
||||||
let count = 1;
|
// Iterating through the array
|
||||||
for (let i = 1; i < n; i += 2) {
|
for (let i = 2; i < n; i++) {
|
||||||
if (primeCheck(i)) {
|
if (primes[i]) {
|
||||||
let flag = true;
|
let curr = String(primes[i]);
|
||||||
let circularNum = i.toString();
|
let tmp = 1; // tmp variable to hold the no of rotations
|
||||||
for (let j = 1; j < i.toString().length; j++) {
|
for (let x = rotate(curr); x != curr; x = rotate(x)) {
|
||||||
circularNum = circularNum.substring(1) + circularNum.substring(0, 1);
|
if (x > n && primes[x]) {
|
||||||
if (primeCheck(Number(circularNum)) === false) {
|
continue;
|
||||||
flag = false;
|
}
|
||||||
|
else if (!primes[x]) {
|
||||||
|
// If the rotated value is 0 then its not a ciruclar prime, break the loop
|
||||||
|
tmp = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
tmp++;
|
||||||
|
primes[x] = 0;
|
||||||
}
|
}
|
||||||
if (flag) {
|
count += tmp;
|
||||||
count++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return count;
|
return count;
|
||||||
};
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
Reference in New Issue
Block a user