0

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...]

  • 1
    You want more than "the number of days ... is a multiple of 7". You also want that either both or noe of $Y_1$ and $Y_2$ are leap years. Next, $\lfloor \frac Y 4\rfloor$ roughly counts the number of leap years before and including the year $Y$. You shoul dwork with $\lfloor \frac{Y-1}4\rfloor$ (and similarly for the century corrections; then again, your floors work when you want to make the differnce between the first s of March (instead of January) a multiple of $7$ ... – Hagen von Eitzen Jan 09 '19 at 17:18
  • Thanks! I had not considered that, so even my numerical calculations were wrong. I will fix them and edit the OP. – user6376297 Jan 10 '19 at 07:17

3 Answers3

1

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.

fleablood
  • 124,253
  • Thanks! I marked your answer as accepted because in the end it's the closest to a practical implementation, where I only need to separate a few cases. – user6376297 Jan 10 '19 at 07:54
0

Hint:

There are seven days in a week. Every fourth year is a leap year. Thus your upper bound is that the calendar repeats itself every $28$ years.

Is there a circumstance that will allow the repeat to happen earlier? To do this, two seperate years must both start on the same day and be either both leap or both not.

Rhys Hughes
  • 12,842
0

The following is not a direct answer to the exact question that was asked. It's meant to be an exposition of some things that a person asking that question might benefit from thinking about.

What "closed form" is is a bit of an open question.

The kinds of things we often see touted as "closed-form" solutions are things like the quadratic equation and the antiderivative of $\sin^4 x.$ Calling something "closed form" in these cases often means we're willing to include some transcendental functions such as sines or logarithms in the formula, even though those are hard to compute to high precision in practice and even though their exact values are expressed by infinite series.

Technically, a closed form expression could span multiple pages as long as it can be fully evaluated in a bounded number of operations, where each operation is selected from some finite set that we consider to be acceptable "closed form" operations (for example, addition, division, taking the cube root of a number, or taking the sine of an angle).

The Gregorian calendar repeats exactly every $400$ years. Given any year number $Y$, you can take the remainder when $Y$ is divided by $400,$ and plug this into a lookup table that gives you the number of years since the exact same calendar occurred (same day of the week for January first, same number of days in February) and the number of years until the next time the exact same calendar occurs.

It seems to me that this is a closed-form solution. You could even rewrite the table as an expression involving floor functions and arithmetic operations, although I think this would be pointlessly complicated and much uglier (and harder to use) than a straightforward table.

I think (though I am not sure) that you could even encode the lookup table as a sum of finitely many sinusoidal functions with periods that divide $400.$ After all, the sum of functions only has to match the required output values at $400$ points; it can do anything you want in between.

All this notwithstanding, there might be a reasonably "nice" closed-form expression that is more compact than the lookup table and not terribly much more difficult to apply. I suspect, however, you will have a much "nicer" expression if you're willing to accept something like $$f(\text{closed-form expression}, y) \text{ where } f(x, y) = \text{another closed-form expression}$$ as a closed form.

My assessment

If the point is to be clever, one might enjoy developing a formula that used just a few simple numeric operations to get the answer. I don't know if there exists such a formula that is as straightforward as Gauss's algorithm for predicting the day of the week on which January 1 falls in the year $Y$: $$ (1 + 5((Y-1) \bmod 4) + 4((Y-1) \bmod 100) + 6((Y-1) \bmod 400)) \bmod 7 $$ I think it's more likely that the "clever" way will take the result of one such formula and use that as input for another formula, as I suggested above. This may or may not make sense as a practical algorithm, but if you're into mathematical puzzles it could be fun to explore.

If the point is to be practical, it depends on the application, especially if you want to automate the calculation in a computer. In software, my preference is for easy-to-understand, unless it's in inside a time-critical loop, in which case my preference is for fast.

A lookup table (preceded by a "mod" function) is about as fast as such a calculation can be.

But if it isn't time-critical, I think the easiest-to-understand method is to use a function $f(x)$ that returns $2$ if $x$ is a leap year and $1$ otherwise. Related functions are discussed in the answers to https://stackoverflow.com/questions/23188795/leap-year-function-with-only-two-conditions and https://stackoverflow.com/questions/11621740/how-to-determine-whether-a-year-is-a-leap-year. Then, given an input year $y,$ add up the values of $f(x)$ for $x = y, y + 1, y + 2, \ldots$ until the result is divisible by $7$ and $f(x) = f(y)$ (that is, $x$ and $y$ are either both leap years or both common years). If that first happens when $x = y',$ then there are $y' - y + 1$ years from $y$ until the next time the calendar repeats.

A similar method (but iteratively setting $x = y - 1, y - 2, \ldots$) finds the number of years since the last time the same calendar occurred.

Additional note

A related question is what is the formula for determining the next year in which a given month/day will occur on a specific weekday. That question is not quite the same as yours because you need to have the same leap-year status as well as the same day of the week for January 1. (If your input is a leap year, however, the answer to your question is the number of years until February 29 next falls on the same weekday.) My answer to that question was a lookup table, but the lookup table is generated from a relatively small number of given values and a few very simple recursive formulas rather than a list of $400$ numbers. A similar approach would work for your problem as well.

David K
  • 98,388
  • Thank you, I take your point. In practice one wants to write some script that does the calculation, and in my naïve view I thought a script containing iterations was less elegant than a formula. – user6376297 Jan 10 '19 at 07:52
  • @user6376297 It depends. Sometimes the efforts we make to avoid iteration are rewarding, sometimes they're more trouble than they're worth. I have added some discussion to the answer. – David K Jan 10 '19 at 13:55
  • I like your suggestion about $f(x)$. Similar to what I did, but more clever. If I take the 'formula' approach I need to separate several cases (nested if statements are needed), so you're right, it may end up being too complicated for its own good. I will still try it out, editing my original post, following up from fleablood's suggestion above. – user6376297 Jan 10 '19 at 16:01
  • There's an intrinsic benefit to exploring different approaches regardless of which one you actually end up using in "real life". Best wishes for your investigations! – David K Jan 10 '19 at 16:04