# project euler – problem 49

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**YGC » R**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another. There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence. What 12-digit number do you form by concatenating the three terms in this sequence?

^{?}View Code RSPLUS

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 | n <- 10^4:10^3 prime <- n[gmp::isprime(n) != 0] pl <- lapply(prime,function(i) unlist(strsplit(as.character(i), split=""))) flag <- 0 for (i in seq_along(pl)) { x <- pl[[i]] maxP <- prime[i] pl <- pl[-i] prime <- prime[-i] idx <- unlist(lapply(pl, function(i) all(x %in% i) & all(i %in% x))) idx <- which(idx) if (length(idx) >= 2) { sel <- prime[idx] diff <- maxP-sel for (j in 1:length(diff)) { m <- which(diff == 2* diff[j]) if (length(m) >= 1) { minP <- sel[m[length(m)]] midP <- sel[j] flag <- 1 break } } } if(flag) { break } } ans <- paste(c(minP, midP, maxP), collapse="") print(ans) |

> system.time(source("problem49.R")) system.time(source("problem49.R")) [1] "296962999629" user system elapsed 0.25 0.00 0.25

#### Related Posts

To

**leave a comment**for the author, please follow the link and comment on their blog:**YGC » R**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.