# To calculate how many times a certain year repeats itself in the calendar within a given year range

Let's say I have a year $$Y$$. I wanna know how can I calculate the number of times the calendar configuration of the year $$Y$$ repeats itself in the year range $$[A, B]$$.

Is there any method to it without brute force, in a pretty efficient way?

NOTE: I'm talking about the Gregorian Calendar.

• Can you precise what you mean by configuration ? I suppose it is the week day corresponding to each month day. How do you consider leap years ? for instance you want every day of the year to have the same week day or only the 1-1 ? – Optidad Feb 13 '20 at 9:03
• @Optidad By configuration I mean the exact same calendars. For example, consider years 2003, 2014, 2025. The years have the exact same calendar. Leap years are considered to be those years which are divisible by 400, and if it isn't divisible by 400, then it should be divisible by 4 but not divisible by 100. For example, 2400 is a leap year but 2300 isn't. – UrbanCentral Feb 13 '20 at 9:53

Basically, the "configuration" of a year, if I understand well can be described as a state $$Y_i = (D_i, L_i)$$ with $$D_i$$ the weekday of January the $$1^{st}$$ (0 to 6 for monday to sunday) and $$L_i$$ which is 1 if $$Y_i$$ is a leap year and 0 else.

So, comparing the state of 2 years, you can decide wether they have the same configuration.

As a year has 365 days (366 for a leap year) and $$365 \mod 7 = 1$$ (2 for a leap year), there is a simple relation:

$$D_{i+1} = D_i+L_i+1 \mod 7$$

As there is one leap year every four years (without 100-exception), it is interesting to also notice there is a 5-step on weekday every 4 years:

$$D_{i+4} = D_i+5 \mod 7$$

Now, how to not loop on all year ?

Let's consider leap years are all years divisible by 4 without exception. Then, from a starting year $$Y_0$$, we seek the next year with same configuration $$Y_s$$. Let's consider all possibles values of $$M_0 = Y_0 \mod 4$$ :

• $$M_0 = 0$$, then $$Y_0$$ is a leap year, as $$GCD(5, 7) = 1$$, 7 of the 5 steps (28 years) are necessary to reach the same configuration: $$Y_s = Y_0 + 28$$ and $$M_s = 0$$
• $$M_0 = 1$$, then $$Y_s = Y_0 + 6$$ and $$M_s = 3$$
• $$M_0 = 2$$, then $$Y_s = Y_0 + 11$$ and $$M_s = 1$$
• $$M_0 = 3$$, then $$Y_s = Y_0 + 11$$ and $$M_s = 2$$

Thus, if the considered year is a leap year, you have exactly the same configuration every 28 years, else, you have 3 times the same configuration every 28 years. In the latter case, the $$B-A \mod 28$$ reamaining years may be simply considered with above rule.

To consider the 100-exception, you may do the same work adding an extra variable modulo 100 (quite more laborious).

Optidad’s answer is almost perfect, but only if you ignore week numbers.

If the first four days of a year or more fall into the same week, then they are in the first calendar week of the year. But if only one, two, or three of the first days fall into the same week, then they are in the last calendar week of the previous year.

The last calendar week of the previous year is usually the 52nd week. There is an exception if the previous year was a leap year, and it had the first four days in the first calendar week, and the last four says in the last (53rd) calendar week.

So you have fifteen, not fourteen different configurations. If exactly the first three days are in the same calendar week, then you have a different configuration if the previous year was a leap year.

One more complication: The rules what is a “calendar week” and what is the first day of a week are different from country to country. In some countries the week is from Sunday to Saturday, in others it is Monday to Sunday. In the first case the exception is a year starting on Thursday after a leap year, in the second case it is a year starting on Friday. Lucky enough we don’t have consecutive leap years.