Brainy calendar building blocks

  Today I accidentally discovered a very mysterious little toy, which can be used to query the day of the week on a given date. Twist the year to the corresponding month, and you can check the specific day of the week for that month. It suddenly felt incredible.

Unexpectedly, I started to figure out the principle again: so I summoned the long-dormant Zeiler formula and a series of its transformations. I don’t want to say anything else, but the formula expressed by this toy looks closest to the following one:weekday = [C//4 - 2*C + y + y//4 + 13 * (M+1) // 5 + d - 1] % 7

C is the century minus one, y is the last two digits of the year, M is the month, and d is the day. January and February should be
calculated based on the 13th and 14th months of the previous year. At this time, C and y will be calculated based on the previous year.

And I prefer the following easy-to-understand formula:

weekday = [(Y-1) + (Y-1)//4 - (Y-1)//100 + (Y-1)//400 + 26 * (M+1) // 10 + d] % 7

Y is the year, M is the month, and d is the number of days. January and February should be calculated based on the 13th and 14th months of the previous year. At this time, the value of Y is based on the previous year.
The calculated number is divided again by 7, and the remainder is the day of the week. For example, if the remainder is 0, it is Sunday.

This formula is a rule, and this rule was actually refined and summarized by an ancient wise man.

To deduce it, we first need another date as a fixed point to know what day of the week it is. Here, the last day 12-31 BC is a Sunday, which is implicitly chosen as the anchor.

Next, if you use a lengthy but easy-to-understand algorithm, calculate how many days there are until that day. Then mod 7, and the remainder can be used to get the day of the week.

(Y-1)*365+(Y-1)//4-(Y-1)//100+(Y-1)//400+(early days of this year)

For example, on the day of writing, September 3, 2022 , it is

(2022-1)*365 + (2022-1)//4- (2022-1)//100 + (2022-1)//400 + (31+28+31+30+31+30+31+31+3)=738401

This thing can be divided by 7 and the remainder is 6, so it is a Saturday.

I call the above calculation the narrative of logic itself; the characteristic is that the calculation process is to tell things straightforwardly. Next comes the metamorphosis of mathematics. People call these transformations and simplifications the beauty of mathematics.

You must keep in mind that the basis of this algorithm is to find the remainder of 7. So in order to simplify elegantly (the perverted part of mathematics), the logic of this story began to be simplified again by looking for rules:

(Y-1)*365 = (Y-1)*(364+1) #Because 364=52*7

Further: 52*7*(Y-1) + (Y-1); So here I actually only want (Y-1)//7, because the previous part is definitely divisible by 7, so I just throw it away.

The rest is due to leap years, because 100 years is not a leap year, but 400 years is a leap year. The central idea of ​​this series of formulas for accumulating days is to round and simplify the number 7.

W = (Y-1) + (Y-1)//4 - (Y-1)//100 + (Y-1)//400 + D

Next, it’s time to calculate the early days of the year, and we start to look at the monthly pattern. First of all, remember that if this month is divisible by 7, then it is a complete worship cycle. We know that the number hanging around 30 and divisible by 7 is 28, then we have,

month January February March April May June July August September October November December
More than 7 months 3 0 or 1 3 2 3 2 3 3 2 3 2 3
The accumulation of 7 remaining months 3 3 or 4 6 or 7 8 or 9 11 or 12 13 or 14 16 or 17 19 or 20 21 or 22 24 or 25 26 or 27 29 or 30

When we get here, we can see that from March to December, there are 10 consecutive months of stability, a total of 26 days. If you want to break it down more carefully, you will see two 32323 from March to December. The remainders of January and February are placed at the tail and become 13 and 14. In fact, they are regarded as serial numbers 13 and 14. But the year will be counted as the serial number of the previous year. The reason for this attempt here is the uncertainty of February. If it can be regarded as the last month, there is no need to discuss ordinary years and leap years on a case-by-case basis, and there will be fewer unknown variables in the formula.

March April May June July August September October November December 13 months (i.e. January of the next year) 14 months (i.e. February of the next year)
3 2 3 2 3 3 2 3 2 3 3 The rest doesn't matter
3 5 8 10 13 16 18 21 23 26 29

Please remember, because this matter itself is to find the divisibility relationship between the days from the last day BC to the present and 7, so when January and February are moved to the end of the year, it is convenient to calculate the accumulated days exceeding 28. OK. Although it looks abstract and disgusting, the essence is to follow the situation of 7 to 0. The days that are squeezed out by 7 in the year part have already been calculated above.

After the parts of the year that are divisible by 7 are not taken into account, what is left is the excess at the end of each month and the current month:

accumulated = 13 * (M+1) // 5; (M\geq 3)

It can be understood that the downward rounding operation is used to realize the cycle of 13 days accumulated corresponding to 5 consecutive months. By coincidence, January is also 13. It is also a 3, which is the position where the next cycle of this stable two-part cycle (32323 32323) of more than 28 starts, without destroying it. February, that is, 14 months, has come to an end again.

As a side note, I always feel that this accumulated part has some meaning in getting the answer to the calculated formula. Because after all, as long as it makes sense for M to be between [3,14], that's fine. After all, this is just a limited algorithm in the real world, not a purely mathematical problem .

Finally, the days squeezed out by 7 in the previous years are put together with the days squeezed out by 7 this year, and divided by 7 again:weekday = [(Y-1) + (Y-1)//4 - (Y-1)//100 + (Y-1)//400 + 26 * (M+1) // 10 + d] % 7

Let me sigh, a mechanical watch with a permanent calendar is not cheap to buy, but its underlying logic can be expressed with a handmade toy that only costs a few yuan. How interesting it is. In the picture of the toy at the top, you can also see from its disk that it is constantly mod 7 mod 4. I really can’t afford to take the time to explain the operation of the toy itself thoroughly, which is a pity.

I feel the upper limit of my abilities of understanding and expression at this moment.

Thanks for this article's lead: How to quickly calculate the day of the week a date is_ljx0305's blog-CSDN blog

Guess you like

Origin blog.csdn.net/u011410413/article/details/126680139