FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript/Project-Euler/Problem035.js at master · TheAlgorithms/JavaScript · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
TheAlgorithms
/
JavaScript
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
5.8k
Star
34.2k
Code
Issues
21
Pull requests
192
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript
/
Project-Euler
/
Problem035.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
39 lines (35 loc) · 1.27 KB
Breadcrumbs
JavaScript
/
Project-Euler
/
Problem035.js
Copy path
File metadata and controls
39 lines (35 loc) · 1.27 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/**
* Problem 35 - Circular primes
*
*
@see
{
@link https://projecteuler.net/problem=35
}
*
* 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 one million?
*
*
@author
ddaniel27
*/
import
{
sieveOfEratosthenes
}
from
'../Maths/SieveOfEratosthenes'
function
problem35
(
n
)
{
if
(
n
<
2
)
{
throw
new
Error
(
'Invalid input'
)
}
// Get a list of primes without 0, 2, 4, 5, 6, 8; this discards the circular primes 2 & 5
const
list
=
sieveOfEratosthenes
(
n
)
.
filter
(
(
prime
)
=>
!
prime
.
toString
(
)
.
match
(
/
[
0
2
4
5
6
8
]
/
)
)
const
result
=
list
.
filter
(
(
number
,
_idx
,
arr
)
=>
{
const
str
=
String
(
number
)
for
(
let
i
=
0
;
i
<
str
.
length
;
i
++
)
{
// Get all rotations of the number
const
rotation
=
str
.
slice
(
i
)
+
str
.
slice
(
0
,
i
)
if
(
!
arr
.
includes
(
Number
(
rotation
)
)
)
{
// Check if the rotation is prime
return
false
}
}
return
true
// If all rotations are prime, then the number is circular prime
}
)
return
result
.
length
+
2
// Add 2 to the result because the circular primes 2 & 5 were discarded
}
export
{
problem35
}
Back
|
FazBrowse Home
|
New Git URL