How can we find all of the prime-number dates in a given year?
This is fairly simple in R. We’ll use the year 2017 as an example, and we’ll use American date order (ddmmyy). But the general approach can easily be adapted for other date formats.
First let’s find the prime-number dates for March 2017, in order to establish the basic method. To test whether a number is prime, we’ll use the isPrime()
function from the numbers
package.
Create the sequence 1 to 31 as characters. Paste ‘3’ to the beginning of each of these, and ‘17’ to the end (with no space in between), and then convert the resulting strings back to numeric, stored in the variable march
.
Then use march
as the argument to isPrime()
and use that result as the index to march
, which will return just those numbers in march
that are prime.
library(numbers)
thirtyone <- as.character(1:31)
march <- as.numeric(paste(rep("3",31), thirtyone, rep("17",31), sep=""))
march[isPrime(march)]
[1] 3217 3517 3617 3917 31517 31817 32117 32717 32917
So there are 9 dates in March 2017 that are prime numbers.
The method can be easily extended to the entire year.
First, create vectors of day numbers (as characters) for the three possible lengths of month (31, 30, and 28). We can ignore leap years here because they are even numbers, and any date format that ends with the year will not have prime-number dates in even-numbered years. But it would not be difficult to adapt the code below for date formats like yyyymmdd and to add code for the case of February in leap years, which has 29 days.
thirtyone <- as.character(1:31)
thirty <- as.character(1:30)
twentyeight <- as.character(1:28)
Create a year
variable, so that we can easily use this code for any odd-numbered year just by changing this variable, and set year
to “17”.
year <- as.character(17)
Now create a for
loop to cycle through each month (by number) and print out the prime-number dates for that month. We use the cat()
function instead of print()
in order to suppress the initial index numbers and to have better control over line breaks.
We’ll also keep a vector of all prime dates that we will count, sort, and print at the end.
allprimedates <- as.numeric(NULL)
for (n in 1:12)
{
month <- as.character(n)
if (n %in% c(1, 3, 5, 7, 8, 10, 12))
{
thismonth <- as.numeric(paste(rep(month, 31), thirtyone, rep(year, 31),sep=""))
}
if (n %in% c(4, 6, 9, 11))
{
thismonth <- as.numeric(paste(rep(month, 30), thirty, rep(year, 30),sep=""))
}
else
{
thismonth <- as.numeric(paste(rep(month, 28), twentyeight, rep(year, 28),sep=""))
}
thismonthprimes <- thismonth[isPrime(thismonth)]
cat(month.abb[n],"\n")
cat(thismonthprimes,"\n")
allprimedates <- c(allprimedates, thismonthprimes)
}
Jan
1117 1217 11117 11317 11617 11717 12517
Feb
2417 2617 2917 21017 21317 21517 21617 21817 22717 22817
Mar
3217 3517 3617 3917 31517 31817 32117 32717
Apr
4217 4517 4817 41017 41117 41617 42017
May
5417 5717 51217 51517 51817 52517 52817
Jun
6217 6317 6917 61417 61717 62017 62417 62617
Jul
7417 7517 7717 7817 71317 71917 72617 72817
Aug
8117 8317 81017 81517 81817 82217
Sep
9817 92317 92717
Oct
101117 101917 102217 102317
Nov
11117 11317 11617 11717 111217 111317 113017
Dec
12517 12917 122117
In all there are 78 prime-number dates in this year.
In numerical order they are:
[1] 1117 1217 2417 2617 2917 3217 3517 3617 3917 4217 4517
[12] 4817 5417 5717 6217 6317 6917 7417 7517 7717 7817 8117
[23] 8317 9817 11117 11117 11317 11317 11617 11617 11717 11717 12517
[34] 12517 12917 21017 21317 21517 21617 21817 22717 22817 31517 31817
[45] 32117 32717 41017 41117 41617 42017 51217 51517 51817 52517 52817
[56] 61417 61717 62017 62417 62617 71317 71917 72617 72817 81017 81517
[67] 81817 82217 92317 92717 101117 101917 102217 102317 111217 111317 113017
[78] 122117