Traditional Culture Encyclopedia - The 24 Solar Terms - Programming ideas of perpetual calendar
Programming ideas of perpetual calendar
Finally, attach a copy ... knowing the address I answered before.
/question/9433604.html
A perpetual calendar, but not a lunar calendar.
How to calculate the day of the week?
Published in 2005-10-121:43: 00.
How to calculate the day of the week?
-Zeiler formula
What is a certain day in history? What day is the future? There are many formulas about this problem (two general formulas and some subsection formulas), among which Zeiler formula is the most famous.
That is w = y+[y/4]+[c/4]-2c+[26 (m+1)1+d-1.
The symbols in the formula have the following meanings: w: week; C: Century-1; Y: year (two digits); M: month (m is greater than or equal to 3, but less than or equal to 14, that is, in Chuller's formula, 1 and February in one year should be regarded as 13, and 14 in the previous year, for example, 1 in 2003 should be regarded as 2002. D: day; [] stands for rounding, that is, only the integer part is taken. (c is the number of centuries minus one, y is the last two digits of the year, m is the month, and d is the number of days.
65438+ 10 and February are calculated according to the previous year's 65438+March and 65438+April, when C and Y are calculated according to the previous year. )
The calculated w is divided by 7, and the remainder is the day of the week. If the remainder is 0, it is Sunday.
Take 65438+2049 10 month/day (1 00th National Day) as an example, the process is as follows:
Zeiler formula: w = y+[y/4]+[c/4]-2c+[26 (m+1)1+d-1.
=49+[49/4]+[20/4]-2×20+[26× ( 10+ 1)/ 10]+ 1- 1
=49+[ 12.25]+5-40+[28.6]
=49+ 12+5-40+28
=54 (divided by 7 and 5)
That is, June 2049 10( 100 National Day) is Friday.
When is your birthday (when you were born, this year, next year)? Might as well have a try.
But the above formula only applies to the situation after 158210/0/015 (at that time, the Pope changed the julian calendar formulated by Julius Caesar to the Gregorian calendar, which is the Gregorian calendar used today).
Deduction of the process: (those who are not interested in reasoning can skip it)
The weekly system is a system with an ancient tradition. It is said that because the Bible Genesis stipulates that God used six.
This day is Genesis, and the seventh day is a rest day, so people take seven days as a cycle to arrange their work and life.
Live, Sunday is a rest day. From a practical point of view, it is also appropriate to take seven days as a cycle. place
Although China's traditional work cycle is ten days (such as "ten breaks" in Wang Bo's Preface to Wang Tengting), that is to say,
Officials work every ten days and rest on the tenth day), but later they also adopted the Western Zhou system.
In our daily life, we often encounter the problem of knowing what day it is today. Sometimes, we want to know
What day is a certain day in the history of Taoism? Usually, the effective way to solve this problem is to look at the calendar, but we never do this.
Carry a calendar with you, not to mention a perpetual calendar with a history of thousands of years. If you want to engage in computer programming,
It is even more unrealistic to calculate the day of the week in advance and store the perpetual calendar. Is there any way to get through at this time?
What formula did you adopt? What day is today?
The answer is yes. In fact, we often do this. Let me give you a simple example first. For example, you know what?
Since 1 in May 2004 is Saturday, it is not difficult to calculate that 3 1 in May 2004 is World No Tobacco Day.
Let's go We can count from 1 to 3 1 for several weeks at the same time, and finally we can calculate that May 3 1 is Monday.
In fact, use mathematics to calculate, without snapping your fingers. We know that a week is a seven-day cycle, so May 1 is a star.
Seven days later, on May 8th, it was also Saturday. On the date, 8- 1=7 is a multiple of 7. Similarly, May 15
May 22nd and May 29th are also Saturdays, and the difference between their dates and May 1 is 14, 2 1 and 28, respectively.
Is a multiple of 7. May 3 1? 3 1- 1=30. Although it is not a multiple of 7, if 3 1 is divided by 7, the remainder is 2.
In other words, the week of May 3 1 is two days after the week of May 1. Two days after Saturday is Monday.
This simple calculation tells us a basic idea of calculating the week: first of all, we must know the day to be calculated.
What day was the day before? Taking this day as the calculation standard is equivalent to a calculation.
"Origin" Secondly, I know how many days there are between the date I want to calculate and this definite date. Divide this date by 7.
The remainder indicates the day after the week to be calculated. If the remainder is
0, indicating that the weeks of these two days are the same. Obviously, if this day as the "origin" is chosen as Sunday, then
The remainder is exactly equal to the day of the week, so the calculation is more convenient.
However, directly calculating the number of days between two days is still cumbersome. For example, July 29th, 1982, May 2004.
The interval of 1 day is 7947 days, which cannot be calculated at once. Including three periods: 1,1July 29, 982.
Days remaining in one year after this date; 2. 1983-2003, all the days of the 21st year; Third, since 2004.
Days from New Year's Day to May 1. The second paragraph is easier to calculate. Equal to 2 1 * 365+5 = 7670 days. The reason for adding it is.
5, because there are five leap years in this period. The first and third paragraphs are more troublesome, such as the third paragraph, which needs to be played.
Add up the days in the first four months of May and add the date value, that is, 31+29+31+30+1=122 days. Similarly, the first one
A cycle needs to add up the days of five months after July and the remaining days in July, and a * * * is 155 days.
So the total number of days between * * * is122+7670+155 = 7947 days.
Think about it carefully, if the date of the "origin" date is 65438+February 3 1, then the first period is also 1.
For the whole year, in this way, the first phase and the second phase can be combined, and the sum of the whole year is exactly equivalent to two.
The year difference between two dates is reduced by one. If the "origin" date is further selected as 1 65438 BC+31February (or astronomy
The total number of the whole year is exactly the year of the day you want to calculate MINUS one. Scientists use February 0 as 65438+3 1. this
After simplifying the sample, you only need to calculate two time periods: one is the total number of days in so many whole years; Second, the day you want to count is this day.
What day of the year? Coincidentally, according to the year and month setting of the Gregorian calendar, this is pushed backwards, and BC165438+February 3 1 is just right.
Sunday, that is to say, the remainder of the total number of days divided by 7 is exactly the day of the week. So the question now is
There is only one: how many leap years there are in so many whole years. This requires understanding the leap rules of the Gregorian calendar.
As we know, the average Gregorian calendar year is 365 days, and leap year is 366 days. The method of setting leap is that the year divisible by 4 is in
February adds one day, but it is not a leap divisible by 100, but a leap divisible by 400. So, like 1600, 2000, 2400.
Every year is a leap year, and 1700, 1800, 1900 and 2 100 are normal years. According to the Gregorian calendar, BC 1 year is also a leap year.
Therefore, for all the whole years from BC 1 year (or AD 0) 12 3 1 year to a certain Y year.
The number of leap years is equal to
[(Y- 1)/4]-[(Y- 1)/ 100]+[(Y- 1)/400],
[...] means only integer parts are taken. The first item indicates that the number of years divisible by 4 needs to be added, and the second item indicates that it needs to be removed.
The number of years divisible by 100, and the third item indicates the need to add the number of years divisible by 400. The reason why y should be reduced by 1 is
In the example, we get the first formula to calculate the day of the week:
w =(Y- 1)* 365+[(Y- 1)/4]-[(Y- 1)/ 100]+[(Y- 1)/400]+d .( 1)
Where d is the cumulative number of days on this day in a year. The calculated w is 65438+ 0 BC (or 0 AD) 65438+ February.
Days between 3 1 and today. If you divide w by 7, what is the remainder? This day is a day of the week. For example, let's
Calculate 1 in May 2004:
w =(2004- 1)* 365+[(2004- 1)/4]-[(2004- 1)/ 100]+[(2004- 1)/400]+
(3 1+29+3 1+30+ 1)
= 73 1702,
731702/7 =104528 ... 6, and the remainder is 6, indicating that this day is Saturday. This is in line with the facts.
Although the above formula (1) is very accurate, the calculated number is too large to be used conveniently. young people
Think about it. In fact, the number of days w is only used to get the remainder after dividing by 7. This inspired us.
You can't simplify this w value, just find a smaller number with the same remainder and use the terminology in number theory.
Generally speaking, even if you find a smaller positive integer and its congruence, you can still calculate the exact number of weeks.
Obviously, the reason why W is so big is that the first term in the formula (Y- 1)*365 is too big. In fact,
(Y- 1)* 365 =(Y- 1)*(364+ 1)
= (Y- 1) * (7*52+ 1)
= 52 * (Y- 1) * 7 + (Y- 1),
The first term of this result is a multiple of 7, and the remainder is 0 when divided by 7, so the remainder of (Y- 1)*365 divided by 7 is actually
The remainder is equal to Y- 1 divided by 7. This relationship can be expressed as:
(Y- 1)*365 ≡ Y- 1 (mod 7)。
Where ≡ is the symbol of congruence in number theory, and mod 7 means using 7 as the module (i.e. divisor).
In this case, the numbers on both sides of the ≡ sign are identical. Therefore, (Y- 1) can be used instead of (Y- 1)*365, so that we can get
This paper introduces the famous and most common formula for calculating the day of the week:
w =(Y- 1)+[(Y- 1)/4]-[(Y- 1)/ 100]+[(Y- 1)/400]+d .(2)
Although this formula is easy to use, it is not the best formula, because the calculation of cumulative days d is also numb.
Angry. Is it possible to calculate directly with the number of months and dates? The answer is also yes. Let's take a look at each one.
The number of days in a month is as follows:
Month: 65438+ February, March, April, May, July, August, September, 65438+ October, 65438+ October, 65438+ February.
-
Days: 3128 (29) 313031303131303/.
If you subtract 28(=4*7) from all these days, it will not affect the remainder of W divided by 7. So we can buy another one.
Form:
Month: 65438+ February, March, April, May, July, August, September, 65438+ October, 65438+ October, 65438+ February.
-
Days remaining: 30 (1) 323232323
Average annual accumulation: 3 36811131619 2124 26 29.
Accumulated leap year: 3 479121417 20 22 25 27 30
If we look carefully, we will find that in the five months from March to July, except 1 and February, the rest of the days are March, February, March and February.
3; The number of days in the five months from August to 65438+February is also 3, 2, 3, 2, 3, which is just a repetition. In the corresponding cumulative days,
The difference between the cumulative number of days last month and the cumulative number of days last month minus 28 is this repetition. It is precisely because of this law
The cumulative number of days in existence, normal year and leap year can be easily expressed by a mathematical formula:
╭d; (When m = 1
d = { 3 1+d; (when m = 2) (3)
╰ [13 * (m+1)/5]-7+(m-1) * 28+d+i. (when m ≥ 3)
Where [...] still means taking only the integer part; M and d are the month and day to be calculated respectively; Average year i=0, leap year
I= 1. The expression M≥3 needs to be explained: [13*(M+ 1)/5]-7 is calculated in Table 2 above.
The average cumulative value plus (M- 1)*28 is the total number of days in all months before the month where the date is to be calculated. this is a
Very clever method to realize 3, 2, 3, 2, 3 cycles with integer operation. For example, for May 2004 1, there are:
d =[ 13 *(5+ 1)/5]-7+(5- 1)* 28+ 1+ 1
= 122,
This is the cumulative number of days in May 2004 1 day.
If we make a change and regard 65438+ October and February as "65438+ March" and "65438+ April" of the previous year, it will not only be
However, it conforms to this formula, and because of this, leap day becomes the last day of the last "year" (a * * * has 14 months).
Day has become a part of d, so the influence of flat leap year has been removed, and the formula is simplified as:
d =[ 13 *(M+ 1)/5]-7+(M- 1)* 28+d .(3≤M≤ 14)(4)
The above formula for calculating the day of the week can be further simplified as:
w =(Y- 1)+[(Y- 1)/4]-[(Y- 1)/ 100]+[(Y- 1)/400]+[ 13 *(M+ 1)/5]-7
+ (M- 1) * 28 + d。
Because both -7 and (M- 1)*28 can be divisible by 7, if these two items are removed, the remainder of W divided by 7 remains unchanged.
The formula becomes:
w =(Y- 1)+[(Y- 1)/4]-[(Y- 1)/ 100]+[(Y- 1)/400]+[ 13 *(M+ 1)/5]+d。
(5)
Of course, it should be noted that 65438+ 10 and February have been regarded as 65438+March and 65438+April of the previous year, so the days of 65438+ 10 and February are calculated.
A week, except m, is calculated by 13 or 14, and every year is also reduced by one. For example, June 65438+1 October1in 2004 is a Thursday, so this is used.
A formula to calculate, there are:
w =(2003- 1)+[(2003- 1)/4]-[(2003- 1)/ 100]+[(2003- 1)/400]+[ 13 *( 13+ 1)/5]
+ 1
= 2002 + 500 - 20 + 5 + 36 + 1
= 2524;
2524/7 = 360 ...4. This is practical.
Formula (5) is already a formula for calculating the day of the week from the year, month and day, but it is not the most concise. This year,
There are some ways to improve the handling of copies. Let's use this formula to calculate the week of March in the first year of each century 1 and list it.
The table is as follows:
Year: 1 (40 1, 80 1, …, 2001)1(501,90 1.
-
Week 4 2
====================================================================
Year: 20 1 (60 1,1,…, 2201) 301(701,1.
-
Week: 0 5
As you can see, this week repeats every four centuries. If we add 30 1 (70 1,11,…, 230 1).
The week number of March 1 is regarded as -2 (according to the definition of remainder in number theory, the remainder of -2 and 5 divided by 7 is the same, so it can be
Do this transformation), then this repeating sequence happens to be the arithmetic progression of 4, 2, 0, -2. Therefore, we
Calculate the week of March 1 in the first year of each century, and the following formula can be obtained:
W = (4-C modulo 4) * 2-4. (6)
Where c is the century number of this century minus one, and mod stands for modular operation, that is, finding the remainder. For example, for March 2006, 5438+0
1 day, C=20, then:
W = (4 - 20 mod 4) * 2 - 4
= 8 - 4
= 4.
Substituting Formula (6) into Formula (5), after transformation, we can get:
(Y- 1)+[(Y- 1)/4]-[(Y- 1)/ 100]+[(Y- 1)/400]≦( 4-C mod 4)* 2- 1
(mod 7)。 (7)
Therefore, (y-1)+[(y-1)/4]-[(y-1)/100]+[(y-1) in formula (5).
The day of the week in the first year of each century can be replaced by (4-C mod 4) * 2- 1 This formula says
What comes out is:
w =(4-C mod 4)* 2- 1+[ 13 *(M+ 1)/5]+d .(8)
Use the formula for calculating the date and week of the first year of each century, and calculate the formula for calculating the date and week of other years in this century.
Easy to get. Because in a century, the year ending with 00 is the last year, so there is no need to take the exam again.
Considering the law of "leap for one hundred years, leap for four hundred years", we only need to consider the law of "leap for four years". Use the formula to simulate (1)
Simplifying this method to Formula (2), we can easily get any calculation simpler than Formula (5) from Formula (8).
A day is a day of the week formula:
w =(4-C mod 4)* 2- 1+(y- 1)+[y/4]+[ 13 *(M+ 1)/5]+d .(9)
Where y is the last two digits of the year.
We can further rewrite (4-C mod 4) * 2 if we consider that the modular operation is not a four-arithmetic operation.
Convert to an expression that contains only four operations. Because there is the following relationship between the quotient Q minus one C divided by four and the remainder R of the century:
4q + r = C,
Where r is C mod 4, therefore, there are:
r = C - 4q
= C - 4 * [C/4]。 ( 10)
rule
(4 - C mod 4) * 2 = (4 - C + 4 * [C/4]) * 2
= 8 - 2C + 8 * [C/4]
≦[C/4]-2C+ 1(mod 7)。 ( 1 1)
Substituting the formula (1 1) into (9), we get:
w =[C/4]-2C+y+[y/4]+[ 13 *(M+ 1)/5]+d- 1。 ( 12)
This formula can be used to calculate W by subtracting 1 from the century number, the last two digits of the year, the number of months and the number of days, and then dividing by 7, and the remainder is
The only thing that needs to be changed is to regard 65438+ October and February as 65438+ March and 65438+ April of the previous year.
C and y are based on the year of the previous year. Therefore, it is generally believed that this is the best way to calculate the day of the week.
Formula. This formula was first put forward by the German mathematician Christian Zeiler, 1822-
1899) is derived from 1886, so it is commonly known as Zeiler formula. In order to facilitate oral calculation,
Where [13 * (M+ 1)/5] is often written as [26 * (m+1)10].
Now let's count the week of May 2004 1. Obviously, C=20, y=4, M=5, d= 1, substituted into Le Cai.
Formula, there are:
w =[20/4]-40+4+ 1+[ 13 *(5+ 1)/5]+ 1- 1
= - 15.
Note that negative numbers cannot be calculated according to the customary concept of remainder, but only according to the definition of remainder in number theory. for (the sake of) convenience
To calculate, we can add an integer multiple of 7 to make it a positive number, for example, add 70 to get 55.
Divided by 7, the remaining 6 means that this day is Saturday. This is consistent with the reality and the result calculated by Formula (2).
The fruits are the same.
Finally, it should be noted that the above formulas are all based on the leap rule of the Gregorian calendar.
Yes For julian calendar, Le Cai also introduced the corresponding formula:
w = 5-C+y+[y/4]+[ 13 *(M+ 1)/5]+d- 1。 ( 13)
========================================
(2005- 10-20 22:25:00) - (4575252)
Several algorithms for calculating the day of the week in any day.
Recently, I saw someone asking Zhou algorithm on the forum, so I specially arranged it. These algorithms are all searched from the internet, and the realization of the algorithms is written by me in the project. I hope it will help everyone.
One: Common formulas
w =[Y- 1]+[(Y- 1)/4]-[(Y- 1)/ 100]+[(Y- 1)/400]+D
Y is the number of years, and D is the cumulative number of days this year, that is, what day is this year?
Two: Zeiler formula.
w = y+[y/4]+[c/4]-2c+[26(m+ 1)/ 10]+d- 1
The symbols in the formula have the following meanings: w: week; C: century; Y: year (two digits); M: month (m is greater than or equal to 3, but less than or equal to 14, that is, in Chuller's formula, 1 and February in one year should be regarded as 13, and 14 in the previous year, for example, 1 in 2003 should be regarded as 2002. D: day; [] stands for rounding, that is, only the integer part is taken.
Compared with the general calculation formula, Zeiler formula greatly reduces the complexity of calculation.
Third, the improvement of Zeiler formula.
Author: Feng Siyuan
Compared with another general formula, Zeiler formula greatly reduces the complexity of calculation. But the general calculation formula given by the author seems to be more concise (including the operation process). Now the formula is as follows:
W=[y/4]+r (y/7)-2r(c/4)+m'+d
The symbols in the formula have the following meanings: r () stands for remainder, that is, only the remainder part; M' is the correction number of m. Now we give the correction number of 1 to 12' as follows: (1', 1 0') = 6; (2',3', 1 1')=2; (4',7')=5; 5'=0; 6'=3; 8'= 1; (9', 12')=4 (Note: in the formula given by the author, the year running time y is1'= 5; 2'= 1)。 Other symbols have the same meanings as those in Zeiler's formula.
Four: Kim Larsen calculation formula
I named this formula, haha, I hope you don't take it amiss.
w =(d+2 * m+3 *(m+ 1)/5+y+y/4-y/ 100+y/400)mod 7
In the formula, d represents the number of days in the date, m represents the number of months, and y represents the number of years.
Note: This formula is different from other formulas:
Consider 1 month and February as the 13 month and 14 month of the previous year. For example, if it is 2004- 1- 10, it is converted to: 2003- 13- 10, and it is substituted into the formula for calculation.
- Previous article:Which solar term has the shortest night?
- Next article:Bamboo shoot growing season
- Related articles
- What are the advantages of opening the second pulse of Ren Du?
- What does the solar term mean? Question mark.
- Lantern riddles and collections of ancient Chinese poetry during the Spring Festival
- When does autumn begin?
- What does 29 on the calendar mean?
- China's traditional festival poems
- What delicious new year goods do you need to prepare for the Spring Festival?
- What's so particular about sweeping graves in Tomb-Sweeping Day? Around Tomb-Sweeping Day, some people said they would go to "Society Day". I really don't know when to go.
- What is the most classic blessing about Han provided greetings to 2020?
- How to deal with the "sudden flameout" fault of the engine while driving, and what preventive measures are there?