Last active
August 29, 2015 14:04
-
-
Save nathanjohnson320/adad7194aad315ceecac to your computer and use it in GitHub Desktop.
Find 7 Digit Prime palindrome in Pi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://oeis.org/A000796/b000796.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nathanjohnson320@vm-2:~$ time Rscript pi.R
[1] "9149419"
real 0m3.155s
user 0m3.055s
sys 0m0.075s