How many times in the future after the 1st of January of 2019 will a palindrome date happen?

123 Views Asked by At

I'm trying to solve the following problem:

How many times in the future (before the year 10000) will after the 1st January of 2019 happen a palindrome date. Date is in the dx-my-ymxd forum, and we suggest that $dx\in\{01,...,31\}$.

Could you help me with how to solve this issue? I don't know how to start. Thank you.

2

There are 2 best solutions below

2
On

The following MiniZinc model arrives at $269$ dates:

var 0..3: d;  % 1st digit of day
var 0..1: m;  % 1st digit of month
var 0..9: x;  % 2nd digit of day
var 0..9: y;  % 1st digit of year
var bool: leapYear;
var 2019..9999: year;

%  day must be in 01..31
constraint (d + x) > 0;

constraint (d*10+x) <= 31;

%  month must be in 01..12
constraint (m + y) > 0;

constraint (10*m + y) <= 12;

%  2019 or later
constraint (1000*y + 100*m + 10*x + d) == year;
constraint 2019 <= year;

%  certain months have less than 31 days
constraint ((d*10+x) <= 30) \/ ((10*m+y) in {1,3,5,7,8,10,12});

%  February has 28 days in normal years and 29 in leap years
constraint leapYear == (((year mod 4) == 0) /\ 
                        (((year mod 100) != 0) \/ ((year mod 400) == 0)));
constraint ((10*m+y) != 2) \/
           ((d*10+x) < 29) \/
           (((d*10+x) == 29) /\ leapYear);

output ["\(d)\(x)-\(m)\(y)-\(y)\(m)\(x)\(d)"]

The number of $269$ dates is confirmed by the following C# code:

DateTime dt = new DateTime(2019, 1, 1);
DateTime lastDay = new DateTime(9999, 12, 31);
int count = 0;

for(; dt < lastDay; dt = dt.AddDays(1))
{
    string s = dt.ToString("ddMMyyyy");
    bool palindrome = true;

    for (int i = 0; i < s.Length/2; i++)
    {
        if (s[i] != s[s.Length - i - 1])
        {
            palindrome = false;
            break;
        }
    }

    if (palindrome)
    {
        count++;
    }
}

Console.WriteLine(count);
3
On

To develop Daniel Mathias comment into a way to obtain the same answer as Axel Kemper without a computer:

For each possible value of day and month digit, reversing these gives a year from 2019 onwards except for

  • all dates in January, October or November (92 total)
  • dates in February ending 0 or 1 (5 total: 01,10,11,20,21)

Thus each valid day-month combination except these gives one valid palindromic date except possibly

  • 29th Feb, where we also need it to be a leap year. However, reversing gives 29-02-2092, and 2092 is a leap year.

So the answer is $366-92-5=269$.