circular-prime
Version:
Dummy Example
48 lines • 1.39 kB
JavaScript
;
/*
A number is called a circular prime if it is prime and all of its rotations are primes.
For example, the number 197 has two rotations: 971, and 719. Both of them are prime.
Another example: 1193 is a circular prime, since 1931, 9311 and 3119 all are also prime.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.circular = void 0;
function isPrime(n) {
if (n == 1)
return false;
for (var i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}
function rotate(n) {
var nn = "" + n;
var n2 = nn[nn.length - 1] + nn.substring(0, nn.length - 1);
return parseInt(n2);
}
/**
* @param {number} N
* takes an input number and returns the number of circular primes from 0 to N
*/
function circular(N) {
var outbound = 0;
for (var i = 2; i <= N; i++) {
var isMatch = true;
var check = i;
for (let j = 0; j < ("" + i).length; ++j) {
if (!isPrime(check)) {
isMatch = false;
break;
}
check = rotate(check);
}
if (isMatch) {
console.log(i, 'is a circular prime');
outbound++;
}
;
}
return outbound;
}
exports.circular = circular;
//# sourceMappingURL=index.js.map