Skip to content

Instantly share code, notes, and snippets.

@nathanjohnson320
Last active August 29, 2015 14:04
Show Gist options
  • Save nathanjohnson320/adad7194aad315ceecac to your computer and use it in GitHub Desktop.
Save nathanjohnson320/adad7194aad315ceecac to your computer and use it in GitHub Desktop.
Find 7 Digit Prime palindrome in Pi
pi <- read.csv("./pi.csv", header=FALSE, sep=" ")
dig <- 14000
pistr <- paste(c(pi[1,]$V2, ".", head(pi[-1,], dig-1)$V2), collapse="")
is.palindrome <- function (word) { identical(word, paste(rev(strsplit(word, "")[[1]]), collapse="")) }
is.prime <- function(n) n == 2L || all(n %% 2L:floor(sqrt(n)) != 0)
base <- 1
end <- 7
repeat {
if (is.palindrome(substr(pistr, base, end)) & is.prime(as.numeric(substr(pistr, base, end)))) {
print(substr(pistr, base, end))
break
}
base <- base + 1
end <- end + 1
}
http://oeis.org/A000796/b000796.txt
@nathanjohnson320
Copy link
Author

nathanjohnson320@vm-2:~$ time Rscript pi.R
[1] "9149419"

real 0m3.155s
user 0m3.055s
sys 0m0.075s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment