TITLE: how to find the closest year that had the exact same calendar as a given year - equations involving floor
QUESTION [0 upvotes]: 2019 has the exact same calendar (i.e. all days of the week coincide) as 2013.
I noticed this by simply looking at the actual printed out calendars.
However, this made me wonder how to calculate in general, for a given year $Y_2$, the closest year $Y_1$ that had the exact same calendar.
In essence, I was trying to find $Y_1 < Y_2$, with $Y_1, Y_2 \in \mathbb{N} $ such that the number of days between Jan 1st $Y_1$ (included) and Jan 1st $Y_2$ (not included) was a multiple of 7.
[Not sufficient: see Edit 1 below]
The number of days between those two dates is:
$N_{Y_2-Y_1} = 365 \cdot (Y_2 - Y_1) + number \ of \ leap \ years$
For the number of leap years, I found this post, so  I could write:
$N_{Y_2-Y_1} = 365 \cdot (Y_2 - Y_1) + \lfloor \frac{Y_2}{4} \rfloor - \lfloor \frac{Y_2}{100} \rfloor + \lfloor \frac{Y_2}{400} \rfloor - \lfloor \frac{Y_1}{4} \rfloor + \lfloor \frac{Y_1}{100} \rfloor - \lfloor \frac{Y_1}{400} \rfloor$
I applied this formula to a simple while loop taking $Y_2$ as input, and checking one by one the years before that until it found one for which the number of days was exactly divisible by 7.
This may be satisfactory from a numerical point of view, and it shows that the smallest distance between calendar-identical years can be 5, 6, 7, 11 or 12, with rather different frequencies for the various cases, 6 being the most frequent, followed by 5 and 11, then 12, then 7.
[WRONG: see Edit 1 below]
However, it does not provide a 'closed form' for the calculation of $Y_1$ given $Y_2$.
I looked at posts and other resources describing equations with floor functions (e.g. this one and this one), and while I sort of understood the concept for the examples given in those posts and could reproduce it, I could not quite fit it to my case.
I wonder if anyone could please provide some guidance?
I started with a simplified case, assuming that all years divisible by 4 are leap years (in fact I did not even know that century years not multiple of 400 were not leap years). The equation is then:
$N_{Y_2-Y_1} = 365 \cdot (Y_2 - Y_1) + \lfloor \frac{Y_2}{4} \rfloor - \lfloor \frac{Y_1}{4} \rfloor$
For this quantity to be a multiple of 7, there must be an integer $i$ such that:
$365 \cdot (Y_2 - Y_1) + \lfloor \frac{Y_2}{4} \rfloor - \lfloor \frac{Y_1}{4} \rfloor = 7 \cdot i$
I.e., considering that $365 \cdot (Y_2 - Y_1) = 7 \cdot 52 \cdot (Y_2 - Y_1) + (Y_2 - Y_1)$:
$(Y_2 - Y_1) + \lfloor \frac{Y_2}{4} \rfloor - \lfloor \frac{Y_1}{4} \rfloor = 7 \cdot [i - 52 \cdot (Y_2 - Y_1)]$
The first doubt I have is: given that $i - 52 \cdot (Y_2 - Y_1)$ is an integer, can I replace it by another integer $j$, or does the fact that it contains my variables make this a wrong move?
I tried the methods described in the posts I linked above, namely substituting the quantities in each floor function with the sum of an integer + a 'fractional' quantity $\in [0,1)$, but I got rather knotted up, and in particular I could not eliminate the initial integer $i$, which however is not known a priori.
Could you please comment about my approach / suggest how I should proceed (for the moment focusing on the simplified case)?
Thanks!

EDIT 1 (after post by Hagen v E)
As pointed out by Hagen, even my numerical calculation was wrong, because it only checked that the starting weekday of $Y_1$ was the same as the starting weekday of $Y_2$, not that the years were both leap or both non-leap.
After adding the leap-match check to the script, it turned out (unless I'm mistaken again) that in each 400 years cycle there are:

182 cases where the closest identical year occurs 11 years earlier
109 cases where the closest identical year occurs 6 years earlier
76 cases where the closest identical year occurs 28 years earlier
18 cases where the closest identical year occurs 12 years earlier
15 cases where the closest identical year occurs 40 years earlier

In the simplified case (considering all years divisible by 4 as leap years):

200 cases where the closest identical year occurs 11 years earlier
100 cases where the closest identical year occurs 6 years earlier
100 cases where the closest identical year occurs 28 years earlier


EDIT 2 (putting together the suggestions from the other users)
Following up from bloodflea's post below, and extending the method to the actual case considering non-leap century years.
Please correct me if I'm wrong.
First, I define 3 conditions.
$a : \frac {Y_2}{400} = \lfloor \frac {Y_2}{400} \rfloor$
$b : \frac {Y_2}{100} = \lfloor \frac {Y_2}{100} \rfloor$
$c : \frac {Y_2}{4} = \lfloor \frac {Y_2}{4} \rfloor$
Expanding all possible cases, and taking into account that:
$a \to b \to c$
there are 4 possible (main) scenarios:
$A : c \land b \land a : Y_2 $ is a century leap year (like 2000)
$B : \bar c \land b \land a : Y_2 $ is a century non-leap year (like 1900)
$C : \bar c \land \bar b \land a : Y_2 $ is a non-century leap year (like 1960)
$D : \bar c \land \bar b \land \bar a : Y_2 $ is a non-century non-leap year (like 2019)
Given $Y_2$, I am looking for a function outputting $Y_1$ as defined above.
I define $\Delta = Y_2 - Y_1$.
In each case, each year in $\Delta$ 'brings' $364 = 7 \cdot 52$ days, plus either $1$ day (non-leap) or $2$ days (leap).
Thus $\Delta$ will be a suitable value when the sum of these 'added' days is a multiple of $7$ and both $Y_2$ and $Y_1$ are of the same 'type' (leap or non-leap).
Case A: ($Y_2$ century leap year)
The condition on the number of 'added' days is:
$\Delta + \lfloor \frac {\Delta}{4} \rfloor = 7 \cdot i, i \in \mathbb{N}^+, \Delta < 400$
As $Y_2$ is a leap year, the condition that both years are of the same type is:
$\Delta = 4 \cdot j, j \in \mathbb{N}^+, \Delta < 400$
Putting the two together:
$4 \cdot j + j = 7 \cdot i$
$5 \cdot j = 7 \cdot i$
$j = i + \frac 2 5 \cdot i$
The smallest $i$ for which this is true is $i = 5$, resulting in:
$j = 5 + 2 = 7$
$\Delta = 4 \cdot 7 = 28 < 400$
Case B ($Y_2$ century non-leap year)
The condition on the number of 'added' days is:
$\Delta + \lfloor \frac {\Delta}{4} \rfloor = 7 \cdot i, i \in \mathbb{N}^+, \Delta < 100$
As $Y_2$ is a non-leap year, the condition that both years are of the same type is:
$\frac {Y_1} 4 \ne integer, \Delta < 100$
$\frac {Y_2-\Delta} 4 \ne integer, \Delta < 100$
$\frac {-\Delta} 4 \ne integer, \Delta < 100$
$\Delta \ne  4 \cdot j, j \in \mathbb{N}^+, 0 < \Delta < 100$
I tried defining $\Delta$ as $4 \cdot j + 1$ etc, but I got nowhere, so I just tried out the first few values. $\Delta = 6$ was the first that satisfied the two conditions.
Case C ($Y_2$ non-century leap year)
Two sub-cases:
C.1. $100 \cdot \lfloor \frac {Y_2}{100} \rfloor$ is a leap year, i.e. $\frac 1 4 \cdot \lfloor \frac {Y_2}{100} \rfloor$ is an integer
C.2. $100 \cdot \lfloor \frac {Y_2}{100} \rfloor$ is a non-leap year, i.e. $\frac 1 4 \cdot \lfloor \frac {Y_2}{100} \rfloor$ is not an integer
[to be continued...]

REPLY [1 votes]: First.  Assuming your year is between more than $28$ years away from a year divisible by $100$ by not divisible be $400$.  (This will hold for the years $1829-1871, 1929-2071, 2129-2179$ etc.)  
For these span of years every year with $28$ years before and $28$ years later, it will hold that every four years will be a leap year.
Non-leap years will have $365 = 52*7 + 1$ days so each consecutive year will normally start one day later than the next. However the year after a leap year will occur two days after the previous year.
If you compare year $n$ to year $n + k$ and and if there are $j$  leap years between $n$ and $k$ then the year will start $k + j$ days later.
Every $28$ years the entire calendar system starts over again because $28$ years will have $7$ leap years and $28 + 7 = 35 = 5*7$ so the calendar will start on the same day and will be a leap year if the first year was a leap year and won't be a leap year if the year wasn't a leap year.
So.  Year $n$....
Case 1:  Year $n$ is a leap year.  The calendar will repeat in $28$ years and was the same $28$ years ago.
Case 2: Year $n$ is one year more than a leap year.  $n+6$ will have one leap year between them ($n + 3$) and so $6 + 1 =7$ so calendar $n + 6$ will start on the same day and will not be a leap year so the calendars will be the same.
Year $n-5$ will be a leap year and not the same calendar.  $n -6$ will have two leap years between them $(n-1, n-5)$ and will start $6+2 = 8$ earlier.  $n-11$ will have three leap years between them ($n-1, n-5, n-9$) and so will start $11 + 3 =14 = 2*7$ days earlier and will be the same calendar.
Case 3: $n$ is two years past a leap year.
$n+5$ is not the same date because there is one leap year between them so the calendars or off by $5+1=6$ days.  $n+6$ is not the same calendar.  There is one leap year between the so $6+1 = 7$ and they start on the same day, but $n+6$ is a leap year.  We must go further.  $n+11$ will have $3$ leap years between them ($n+2, n+6,n+10$ and thus will start $11 + 3 = 14=2*7$ days later and will be the same calendar.
$n-5$ isn't the same.  (One leap year and $5$ days isn't a multiple of $7$.)  Nor is $n-6$ (it's a leap year). But $n-11$ will have three leap years $(n-2, n-6, n-10)$ and so will be $11 + 3 = 14$ days offset and the calendars will be the same.
Case 4: $n$ is 3 years past a leap year (like $2019$ is)
Then $n+5$ is a leap year $n+6$ has two leap years between and $n + 11$ will have $3$ leap years ($n+1, n+5, n+9$) and so be offset by $14$ and have the same calendar.
So $2030$ will be the next year with the same calendars.
And $n-6$ will have one leap year between them $n-3$ and so be offset by $6+1 = 7$ days and have the same calender.  So $2013$ had the same.
Monkey Wrench.  Years divisible by $100$ by not by $400$ do not have leap days and they throw the system off.
But again we can calculate those much the same.
{"set_name": "stack_exchange", "score": 0, "question_id": 3067695}