-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircular-Primes.js
More file actions
64 lines (54 loc) · 1.36 KB
/
Circular-Primes.js
File metadata and controls
64 lines (54 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Description:
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.
How many circular primes are there below n, whereas 100 ≤ n ≤ 1000000?
Note:
Circular primes individual rotation can exceed n.
Examples:
circularPrimes(100) should return 13.
circularPrimes(100000) should return 43.
circularPrimes(250000) should return 45.
circularPrimes(500000) should return 49.
circularPrimes(750000) should return 49.
circularPrimes(1000000) should return 55.
*/
function prime(n){
if(n<=1){
return false
}
for(let i=2; i<=Math.sqrt(n); i++){
if(n%i===0){
return false
}
}
return true
}
function check(number){
let num = number.toString();
for(let i=0; i<num.length;i++){
let reverse = parseInt(num.slice(i) + num.slice(0, i));
if(!prime(reverse)){
return false
}
}
return true
}
function circularPrimes(n) {
let nums = []
let counter = 0
for(let i=2;i<=n;i++){
if(prime(i)){
nums.push(i)
}
}
for(let num of nums){
if(check(num)){
counter ++
//console.log(num)
}
}
//console.log(counter+7)
return counter;
}
circularPrimes(100);