Primel is the numeric equivalent of wordle, using 5 digit prime numbers instead of words. This post un-picks this in R.

In our house, we’re fans of wordle, sitting down every evening to keep our streak going. But there is also a number equivalent called primel, using five digit prime numbers instead of five letter words. This, of course, is therefore prefect territory for some R programming. Let’s look at yesterday’s primel challenge in this post.

Like *wordle*, getting the appropriate initial guess is key. It has to be a prime number, and like in *wordle*, it’s good not to repeat digits. The available space of numbers range from 10000 to 99999. Let’s make a vector of these and then filter to just have the prime numbers. The `isprime()`

function from the `matlab`

library is useful here.

This leaves us with 8363 prime numbers.

Now we want to get rid of numbers that repeat a digit. 11113 wouldn’t be a great initial guess as it contains four 1’s. To do this, we’ll split the numbers down to their individual digits, make a table of each one, and discard numbers which have a table entry greater than 1.

```
numbers <- numbers %>%
mutate(x_split = str_split(x, ""))
indices_no_repeats <- map(numbers$x_split, function(x){table(x) %>% max() == 1}) %>% unlist()
numbers <- numbers[indices_no_repeats,]
head(numbers$x)
```

`[1] 10243 10247 10253 10259 10267 10273`

We end up with 2529 prime numbers with unique digits. The first one of these is 10243. So let’s plug that in to today’s primel.

*1*, *0*, and *3* came up as correct digits, but in each case in the wrong place. So let’s try primes that start with *3*, and finish with *01*. To do this, we set up a vector of numbers from 30001 to 40000, with a step size of 100. Then we check which ones are primes. We end up with 17 possibilities. After our initial guess, we can exclude ones that include the digits *2* and *4*, and again we’d prefer ones with no repeat digits. 35801 is the first in our list to satisfy these criteria. Let’s try it.

```
begin <- 30001
end <- 40000
my_step <- 100
numbers <- seq(begin, end, by = my_step)
numbers[isprime(numbers)==1]
```

```
[1] 31601 32401 32801 33301 33601 34301 34501 35201 35401 35801 36901
[12] 37201 37501 38201 38501 39301 39901
```

From these results, we see that *3* and *1* are indeed the first and last digits, and *0* must be the middle digit. Let’s run this again to see which primes satisfy these conditions. Note, the `numbers%%1000`

picks out the last three digits of our numbers, and checking that this is between 0 and 100 means the third last digit must be a 0.

```
begin <- 30001
end <- 40000
my_step <- 10
numbers <- seq(begin, end, by = my_step)
numbers[isprime(numbers)==1 & between(numbers%%1000, 0, 100)]
```

```
[1] 30011 30071 30091 31051 31081 31091 32051 33071 33091 34031 34061
[12] 35051 35081 36011 36061 37021 37061 38011 39041
```

We can exclude numbers with the digits *2*, *4*, *5*, or *8*, and again, we’d like to have a guess with no repeat digits. 37061 looks good, let’s try it.

Now we know that the first three digits must be *360*, and the last digit must be *1*. Let’s generate those primes.

```
begin <- 36001
end <- 36100
my_step <- 10
numbers <- seq(begin, end, by = my_step)
numbers[isprime(numbers)==1]
```

`[1] 36011 36061`

This gives us just two possibilities, but we’ve already ruled out *6* in the fourth position from guess three, so our answer must be 36011. And sure enough.

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Text and figures are licensed under Creative Commons Attribution CC BY 4.0. Source code is available at https://github.com/eugene100hickey/fizzics, unless otherwise noted. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".

For attribution, please cite this work as

Eugene (2022, Feb. 16). Euge: Primel. Retrieved from https://www.fizzics.ie/posts/2022-02-14-primel/

BibTeX citation

@misc{eugene2022primel, author = {Eugene, }, title = {Euge: Primel}, url = {https://www.fizzics.ie/posts/2022-02-14-primel/}, year = {2022} }