Caille Formula What day is it today?

Source: Internet
Author: User
Tags repetition

The reason for knowing the Caille formula is because of a topic that was done at nine degrees today [topic 1043:day of Week]. Previously also had to ask for the day of the week such topics, then the practice is more rough: already know a certain year a certain day is the days of the week, the request is given today is the week, the date and the known date of the difference between the number of days to 7, and then according to the known week to make corresponding adjustments can be. Such a practice is not only cumbersome, but it may be wrong to be inattentive. This time we have a similar problem and hope to try a new method. So in the process of checking data, know the Caille formula. So with Caille formula to solve the problem, although know Caille formula, but in the use of the process is still wrong constantly. In fact, because the understanding of the Caille formula is not deeply caused.

The form of the Caille formula is as follows:w=c/4-2*c+y+y/4+13* (m+1)/5+d-1

The meanings of each parameter are as follows:

W: The Caille formula calculates a number. W 7 modulo: 0-Sunday, 1-Monday, 2-Tuesday, 3-Wednesday, 4-Thursday, 5-Friday, 6-Saturday.

C: Current century-1 (i.e. the first two digits of the year)

Y: Two digits after the year

M: Month

D: Day

The following points need to be noted when using:

(1) The value of W. The value of W is finally calculated to be negative, and negative values for 7 may be different for different environments. Therefore, in order to avoid the negative number of the possible problems, can make a judgement on W, if W is less than 0, with w+=7 to make w positive.

(2) M. The value range of M is [3,14]. Among them, 13, 1 April for the previous year. Therefore, when the month to be asked for December, the month should be added 12 and the year minus one .

The following is an excerpt of the information

The week system is a system with ancient traditions. It is said that since the Bible Genesis stipulates that God has spent six days in Genesis and the seventh day of rest, people have been arranging their work and life for a period of seven days, and Sunday is a day of rest. From a practical point of view, seven days as a cycle, the length is more appropriate.  So while the traditional work cycle in China is 10 days (for example, "Wang Bo" in the Tengwang Pavilion Order), which means that officials work every 10th for a period of 10th, but later also adopted the Western Week system. In our daily life, we often meet the question of knowing the day of the week. Sometimes we want to know the day of the week in history. Usually, the effective way to solve this method is to look at the calendar, but we do not always carry a calendar, it is not possible to carry a thousands of-year perpetual calendar at any time. If you want to calculate the day of the week in computer programming, it is even more unrealistic to put a perpetual calendar in advance.  At this time is there a way to pass what formula, from month to day launch this day is the week? The answer is yes. In fact, we often do so. Let's start with a simple example. For example, knowing that May 1, 2004 is Saturday, then May 31, 2004 "World No Tobacco Day" is not difficult to calculate the week. We can count the fingers from 1st to 31st, and several weeks, and finally the May 31 is Monday. In fact, using mathematical calculations, you can not break the finger. We know that the week is seven days a cycle, so May 1 is Saturday, seven days after the May 8 is also Saturday. On the date, 8-1=7, is a multiple of 7. Similarly, May 15, May 22 and May 29 are also Saturday, their dates and the difference between May 1 is 14, 21 and 28, is also a multiple of 7. What about May 31? 31-1=30, although not a multiple of 7, but 31 divided by 7, the remainder of 2 means that the week of May 31, 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, to know the day before the calculation of a definite day is the day of the week, take this as the standard of reckoning, that is, the equivalent of a calculation of the "origin." Secondly, knowing the difference between the days to be counted and the definite day, with 7 in addition to the difference of this date, the remainder represents the days of the week after the definite day of the week. If the remainder is 0, it means that the week of the two days is the same.  Obviously, if this is the "origin" of the day selected as the Sunday, then the remainder is exactly equal to the days of the week, so that the calculation is more convenient. But the direct calculation of the number of days between two days is still cumbersome. For example, between July 29, 1982 and May 1, 2004, 7,947 days apart, not all of a sudden can be calculated. It consists of three periods of time: one, 1982 7The number of days remaining for the year after 29th, and 1983-2003 for all the 21 full days, and three days from 2004 New Year to May 1. The second paragraph is relatively good, it is equal to 21*365+5=7670 days, the reason is to add 5, because there are 5 leap years in this period of time. The first and third paragraphs are more troublesome, such as the third paragraph, which needs to add up the days of the four months before May plus the date value, which is 31+29+31+30+1=122 days. In the same vein, the first paragraph needs to add up the five months after July, plus the remaining days of July, for a total of 155 days.  So the total number of days separated is 122+7670+155=7947 days. Think carefully, if the "Origin" date is selected as December 31, then the first period of time is a whole year, so that the first and second time can be combined, the total number of full year is equal to two days of the year difference minus one. If the "Origin" day is further selected as December 31, 1 BC (or December 31, 00 A.D. by astronomers), the total number of the whole year is exactly the year minus one of the days to be counted. When this is simplified, only two periods of time are required: one, the total number of days of the year, and two, the day of the year in which the calculation is to be counted. Coincidentally, in accordance with the Gregorian calendar years set, so back, the December 31, 1 BC is exactly Sunday, that is to say, the total number of days divided by 7 is exactly the day of the week. Then there is only one question: How many leap years there are in so many years.  This requires an understanding of the set-up rules for the Gregorian calendar. We know that the common year of the Gregorian calendar is 365 days, and a leap year is 366 days. The method of placing a leap is to be divisible by 4 year in February plus one day, but can be divisible by 100 does not leap, can be divisible by 400 and leap. Therefore, like 1600, 2000, 2,400 years is a leap year, and 1700, 1800, 1900, 2,100 years are common year.  1 BC is also a leap year by Gregorian calendar. Therefore, the number of leap years in the year from 1 (or ad 00) December 31 to a date of a day is equal to [(Y-1)/4]-[(Y-1)/100] + [(Y-1)/400],[...] Indicates that only the integer portion is taken. The first indicates the number of years that are divisible by 4, the second indicates the number of years that are divisible by 100, and the third indicates the number of years to be divisible by 400. Y is going to subtract one, so we get the first formula that calculates 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 in this year. The calculated W is 1 BC (or A.D. 00) December 31The number of days between this day. Divide W with 7, the remainder is a few, this is the day of the week.  Like we're counting. May 1, 2004: W = (2004-1) *365 + [(2004-1)/4]-[(2004-1)/100] + [(2004-1)/400] + (31+29+31+30+1) = 731702,731702/7 = 104528 ... 6, the remainder is six, indicating that the day is Saturday.  This is in line with the facts. The above formula (1), although very accurate, but the calculated numbers are too large, very inconvenient to use. Think about it, in fact, the number of days of the interval w is only to get it divided by 7 after the remainder.  This inspires us whether we can simplify this w value, just to find a smaller number that is the same as its remainder, in terms of numbers, it is to find a small positive integer with its congruence, so that the exact number of weeks can be calculated. Obviously, the reason W is so big is because the first item in the formula (Y-1) *365 is too big. In fact, (Y-1) *365 = (Y-1) * (364+1) = (Y-1) * (7*52+1) = * (Y-1) * 7 + (Y-1), the first item of this result is a multiple of 7, divided by 7 is 0, so (Y-1) *365 divided by 7 is actually equal to Y -1 divided by 7 remainder. This relationship can be expressed as: (Y-1) *365≡y-1 (mod 7). Among them, ≡ is the symbol of the number theory, mod 7 means that in the case of 7 modulus (that is, the divisor), the numbers on both sides of the ≡ number is the same remainder. Therefore, it is entirely possible to replace (Y-1) *365 with (Y-1), so that we get 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 more useful, but not the best formula, because the cumulative days of the calculation of D is also more troublesome. Is it possible to calculate the number of months and the date directly? The answer is yes. We might as well look at the number of days of each month as follows: Month: January February March April May June July August September October November December------------------------------------------------------- -------------------Days: 31 28 (29) 31 30 31 30 31 31 30 31 30 31 if the number of days is subtracted by 28 (=4*7), the remainder of W divided by 7 is not affected. So that we can get anotherOne table: Month: January February March April May June July August September October November December------------------------------------------------------------------------ Days left: 3 0 (1) 3 2 3 2 3 3 2 3 2 3 common Year cumulative: 3 3 6 8 11 13 16 19 21 24 26 29 Leap Year Cumulative: 3 4 7 9 12 14 17 20 22 25 27 30 Careful observation, we will find the removal January and February, March to July, the remaining days of the five months are 3,2,3,2,3;8 months to December, the five-month value is also 3,2,3,2,3, exactly a repetition. In the corresponding cumulative days, the difference between the cumulative number of days after January and the cumulative number of days in the previous January minus 28 is the repetition. It is precisely because of this law that the accumulated days of common year and leap years can be easily expressed in mathematical formulae: ╭d; (when m=1) d = {+ D; (when m=2) (3) ╰[* (M  +1)/5]-7 + (M-1) * + + D + I. (when m≥3) where [...] Still means to take only the integer part; M and d are the months and days of the day they want to count; common year i=0, leap years i=1. The expression for m≥3 needs to be explained: [13* (M+1)/5]-7 calculates the common year cumulative value in the second table above, plus (M-1) *28 is the total number of days of all months before the month of the day to be counted. This is a clever way to use the rounding operation to achieve the 3,2,3,2,3 loop.  For example, on May 1, 2004, there are: D = [13 * (5+1)/5]-7 + (5-1) * 28 + 1 + 1 = 122, which is the cumulative number of days of May 1 in 2004. If we were to make a few more adaptations, taking January and February as the "1 March" and "1 April" of the previous year, it would not only be consistent with the formula, but because it would be the last day of the previous "year" (altogether 14 months), it became part of D, and the effect of the flat leap years was removed, the formula reduced to [* (M+1)/5]-7 + (M-1) * + D. (3≤M≤14) (4) The formula that calculates the day of the week can be further simplified into: W = (Y-1) + [(Y-1)/4]-[(Y-1)/100] + [(Y1)/400] + [* (m+1)/5]-7 + (M-1) * + D. Since 7 and (M-1) * 282 are divisible by 7, the remainder of W divided by 7 is not changed, and the formula becomes: w = (Y-1) + [(Y-1)/4]-[(Y-1)/100] + [(Y-1)/400] + [(m+1)/5] +                                     D (5) Of course, it is important to note that January and February have been treated as 1 March and 1 April for the previous year, so when calculating the days of January and February, the year Y will have to be reduced by 13 or 14, except for M.  For example, January 1, 2004 is Thursday, with this formula to calculate, there is: 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 consistent with the reality. The formula (5) is already a formula for the day of the week from the year, month, and day, but it is not the most concise, and there are improved ways of dealing with the year. Let's first use this formula to figure out the week of March 1, 1 of each century, as follows: Year: 1 (401,801,..., 2001) 101 (501,901,..., 2101)---------------------------------------- ----------------------------Week: 4 2 ==================================================================== Year: 201 ( 601,1001,..., 2201) 301 (701,1101,..., 2301)--------------------------------------------------------------------Week: 0 5 can be seen, every four centuries, this week is repeated. If we consider 301 (701,1101,..., 2301) The week of March 1 as 2 (according to the definition of the remainder in number theory, 2 and 5 divided by 7, the remainder is the same, so this transformation can be done), then this repeating sequence is exactly a 4,2,0,-2 arithmetic progression. Accordingly, we can get the following calculations per century March 1 1Day of the week formula: W = (4-c mod 4) * 2-4. (6) In the formula, C is the century of the century minus one, MoD represents the modulo operation, that is, the remainder.  For example, for March 1, 2001, C=20, then: W = (4-20 mod 4) * 2-4 = 8-4 = 4. Substituting the formula (6) into the formula (5), the transformation can be obtained by: (Y-1) + [(Y-1)/4]-[(Y-1)/100] + [(Y-1)/400]≡ (4-c mod 4) * 2-1 (mod 7). (7) Therefore, the formula (5) in (Y-1) + [(Y-1)/4]-[(Y-1)/100] + [(Y-1)/400] These four items, in the calculation of the first year of the century, the date of the week, you can use (4-c mod 4) * 2-1 to replace. This formula is written to: W = (4-c mod 4) * 2-1 + [* (M+1)/5] + D. (8) with the formula for calculating the date and week of the first year of the century, the formula for calculating the date and week of the other years of the century is readily available. Because in a century, the end of the 00 year is the last year, so there is no need to consider the "100 years not leap, 400 years and a leap" rule, only need to consider the "four years a leap" rule.  By simplifying the formula (1) to a formula (2), it is easy to get a formula (8) that is simpler than the formula (5) to calculate the day of the week: W = (4-c mod 4) * 2-1 + (y-1) + [Y/4] + [* (m+1)/5] + D.  (9), Y is the latter two digits of the year. If we consider that the modulo operation is not arithmetic, we can also change (4-c mod 4) * 2 into an expression containing only arithmetic. Because the century minus one C divided by 4 quotient Q and remainder R has the following relationship: 4q + R = C, where r is C mod 4, so there is: r = c-4q = C-4 * [C/4]. (10) then (4-c mod 4) * 2 = (4-c + 4 * [C/4]) * 2 = 8-2c + 8 * [C/4]≡[C/4]-2C + 1 (mod 7). (11) Bashi (11) substituting (9), get: W = [C/4]-2C + y + [Y/4] + [* (M+1)/5] + d-1. (12) The formula is reduced by one century, the end of the year two, the month and the number of daysCan calculate the W, and then divided by 7, the remainder is a few to indicate the day of the week, the only need to adapt to the January and February as the previous year of 1 March and 1 April, C and Y are based on the years of the year value. Therefore, it is generally accepted that this is the best formula for calculating the day of the week. This formula was first deduced by the German mathematician Christian Caille (Christian Zeller, 1822-1899) in 1886, so the pass is called the Caille formula (Zeller's Formula).  For the convenience of mental arithmetic, [* (M+1)/5] In the formula is also often written as [+ * (m+1)/10]. Now still let's count the week of May 1, 2004, apparently c=20,y=4,m=5,d=1, substituting the Caille formula, there: W = [20/4]-40 + 4 + 1 + [13 * (5+1)/5] + 1-1 =-15. Note that negative numbers cannot be obtained by the concept of the remainder of the habit, but only by the definition of the remainder in number theory. For ease of calculation, we can add a 7 integer to it to make it a positive number, such as adding 70 to get 55. Divided by 7, the remainder of 6, indicating that this day is Saturday.  This is consistent with the fact that the formula (2) calculates the same result. Finally, it is necessary to note that the above formula is based on the Gregorian calendar, which is a set of leap rules to consider. For the Julian calendar, Caille also introduced the corresponding formula is: W = 5-c + y + [Y/4] + [* (m+1)/5] + d-1. (13) In this way, we finally solved the problem of not looking up the calendar to calculate the day of the week.

  

Resources:

Http://bbs.bccn.net/thread-162951-2-1.html

Caille Formula What day is it today?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.