mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2025-12-19 10:07:46 -05:00
1.4 KiB
1.4 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 69373793f5a867f769cde137 | Challenge 152: Circular Prime | 28 | challenge-152 |
--description--
Given an integer, determine if it is a circular prime.
A circular prime is an integer where all rotations of its digits are themselves prime.
For example, 197 is a circular prime because all rotations of its digits: 197, 971, and 719, are prime numbers.
--hints--
isCircularPrime(197) should return true.
assert.isTrue(isCircularPrime(197));
isCircularPrime(23) should return false.
assert.isFalse(isCircularPrime(23));
isCircularPrime(13) should return true.
assert.isTrue(isCircularPrime(13));
isCircularPrime(89) should return false.
assert.isFalse(isCircularPrime(89));
isCircularPrime(1193) should return true.
assert.isTrue(isCircularPrime(1193));
--seed--
--seed-contents--
function isCircularPrime(n) {
return n;
}
--solutions--
function isPrime(n) {
if (n < 2) return false;
for (let i = 2, sqrt = Math.floor(Math.sqrt(n)); i <= sqrt; i++) {
if (n % i === 0) return false;
}
return true;
}
function rotations(num) {
const str = num.toString();
const result = [];
for (let i = 0; i < str.length; i++) {
result.push(str.slice(i) + str.slice(0, i));
}
return result.map(Number);
}
function isCircularPrime(num) {
const rots = rotations(num);
return rots.every(isPrime);
}