There is a sequence 1487, 4817 and 8147 that has three properties:

1. All numbers are primes

2. They are all permutations of each other

3. Each term increases by a certain number (in this case 3330)

Project Euler claims that there are two such sequences with four-digit numbers. Our task is to find the other.

The answer is the three primes with the required properties, pasted together in ascending order.

As usual I’ll load the numbers library. And the first task must be to find all four-digit primes.

```
library(numbers)
cand <- Primes(1000,9999)
```

But we will not need these prime. We already know that answer:

```
cand <- cand[-which(cand %in% c(1487, 4817, 8147))]
```

If one of these four-digit primes is part of such a sequence, there must exist at least two other primes, which are permutations of it.

Lets begin by generating that list

The library gtools provides the function permutations()

```
library(gtools)
listing <- function(x){
b <- unlist(strsplit(as.character(x),""))
c <- permutations(4,4,v=b, set=FALSE)
d <- apply(c, 1, paste, collapse="")
d <- as.numeric(d)
sort(unique(d[d %in% cand]))
}
```

We take a four-digit number as input, converts it to characters via as.character(), and uses strsplit() to get a character vector with for numbers.

Then permutations. The first “4” indicates that the source vector (b) has 4 elements. The second “4” that we want results that contains 4 elements. That we take “b” as the source vector – i.e. the elements from which the permuations should be made. And set=FALSE is a lgical flag. Default is TRUE, and will remove duplicates. That is not what we want, so it is set to FALSE.

Then all the permutations are collapsed to four-digit strings, converted to numeric, and duplicates are removed with unique(), returning a list of all permuations of the digits in the input, that are in the list of candidate primes.

Thats nice. But we know that there should be three numbers in the sequence. This function simply tests if there are at least three numbers in the listing:

```
poss <- function(x){
length(listing(x)) >= 3
}
```

That was the simple part. We can now take all the four-digit primes, excluding the three we already know about, make permutations of them, and see if there are at least three permutations among them, that are prime.

Now it gets a bit more complicated. Given a set of four-digit numbers, we need to locate a subset of three, A, B and C. The differences C-B and B-A should be equal.

There are probably more elegant ways to do that, but this is the way I found.

Given a number x, get all possible permutations of that:

listing(x)

Get all the combinations of three of these numbers:

comb(listing(x),3)

Now we have a matrix, with all the possible combinations in the columns.

Find the differences between the first and the second row, and the second and the third row, for all columns:

apply(apply(combn(listing(x), 3),2,diff)

If the two differences are equal, they fullfil the third property these numbers should have. Therefor we need to find the differende between the two differences. It that is 0, we have our result:

apply(apply(combn(listing(x), 3),2,diff),2,diff)==0])

That gives a logical vector, that is TRUE whenever the difference between the three numbers is the same. We get the numbers by:

combn(listing(x), 3)[,apply(apply(combn(listing(x), 3),2,diff),2,diff)==0]

I’ll define that as a function:

```
corrComb <- function(x){
combn(listing(x), 3)[,apply(apply(combn(listing(x), 3),2,diff),2,diff)==0]
}
```

It would be nice to have that as a logical test as well. I’m running out of ideas for reasonable function names:

```
test <- function(x){
as.logical(sum(corrComb(x)))
}
```

Very simple – if there is a combination fulfilling the requirements, return TRUE.

Now, lets put it all together. I’ll use the library purrr, which allows me to use these nifty pipes.

Taking the candidate primes, we keep the ones that have three or more permutations that are prime, and in our candidate list. Of those, we keep the ones that have fulfill the requirement that the pairwise differences between three of them are equal.

Those are piped to corrComb, that returns the three numbers. They are piped to sort, so we get them in ascending order, and then I paste them together.

```
library(purrr)
answer <- cand %>%
keep(poss) %>%
keep(test) %>%
corrComb%>%
sort %>%
paste(.,collapse="")
```

Lessons learned.

- Those pipes are pretty neat.
- The apply family is also pretty cool
- Read the documentation. I spent some time being confused about the results from permuations() until I discovered the set-flag