Problem 37

The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.

Find the sum of the only eleven primes that are both truncatable from left to right and right to left.

NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
Problem 37 - Project Euler

isprime <- function(n){
  if(n == 1) return(FALSE)
  if(n < 4) return(TRUE)
  if(n%%2 == 0) return(FALSE)
  if(n < 9) return(TRUE)
  if(n%%3==0) return(FALSE)
  r <- floor(sqrt(n))
  f <- 5
  while(f <= r){
    if(n%%f == 0) return(FALSE)
    if(n%%(f+2) ==0) return(FALSE)
    f <- f + 6
  }
  return(TRUE)
}
Rcut1 <- function(n){
  floor(n/10)
}
Lcut1 <- function(n){
  n%%10^floor(log(n,10))
}

count <- 0
limit <- 11
num <- 11
sum <- 0
num.list <- numeric(11)
while(count < limit){
  if(floor(num/10^floor(log(num,10)))%%2==0){
    num + 10^floor(log(num,10))
  }
  if(!isprime(num)){
    num <- num+2
    next
  }
  Rc <- Lc <- num
  frag <- FALSE
  while(Rc > 10){
    Rc <- Rcut1(Rc)
    Lc <- Lcut1(Lc)
    if(!(isprime(Rc) && isprime(Lc))){
      num <- num+2
      frag <- TRUE
      break
    }
  }
  if(frag) next
  sum <- sum + num
  count <- count+1
  num.list[count] <- num
  num <- num+2
}

だめだこんなの…クソすぎる…しんだほうがいい…

要するに左右に桁を追加しながら判定してけってことかな。もうちょい考える。