-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp35.R
More file actions
32 lines (28 loc) · 833 Bytes
/
Copy pathp35.R
File metadata and controls
32 lines (28 loc) · 833 Bytes
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
# [1] "Number of circular primes below one million is 55"
# user system elapsed
# 2.534 0.000 2.514
main <- function(){
pmt <- proc.time()
library(pracma)
p <- primes(1e6)
p <- p[sapply(p, containsComposite)]
p <- p[sapply(p,isCircular)]
print(paste("Number of circular primes below one million is", length(p)+2))#plus 2 and 5
print(proc.time()-pmt)
}
containsComposite <- function(x){
x <- as.numeric(unlist(strsplit(as.character(x),"")))
if( any(x%%2==0) || any(x==5) ){
return(FALSE)
}
return(TRUE)
}
isCircular <- function(x){
x <- as.numeric(unlist(strsplit(as.character(x),"")))
l <- length(x)
x <- c(x,x)
for(i in 2:(l) ){
if(!isprime(as.numeric(paste(x[i:(i+l-1)],sep = "",collapse = "")))){return(FALSE)}
}
return(TRUE)
}