User:Q5968661/sandbox

2024年July月29日

《七夕夜咏》 4/4

$5 • 这 此$||$5 &emsp; 秋 时$• $3 &emsp; 怎$ $65 &emsp; 不&emsp; 酒&emsp;$ $3212 &emsp; 见&emsp;&emsp; 铺&emsp;&emsp;$|$3 &emsp; 霜 张$3 2 1 $01 &emsp; &emsp;这 &emsp;此$|$1 · 夜 刻$• $2 · 怎$ $1 · 不 杯$ $63 &emsp; 见&emsp; 迷&emsp;$|$5 &emsp; 长 茫$• $6 &emsp;$ 5 $6 &emsp; 这 此$56 |$1 · 思 际$• $6 &emsp; 怎 人$ $55 &emsp; 不&emsp; 无&emsp;$$65 &emsp; 必&emsp;$|$3 &emsp; 妄 语$ – – $1 &emsp; 这 此$23 |$2 &emsp; 意 心$• $3 &emsp; 怎$ $65 &emsp; 不&emsp; 月&emsp;$$56 • • 须&emsp; 有&emsp;$|$1 &emsp; 黄 伤$ – – – ||$3 · 今$ $3 · 夕$ $2 ·$ $13 · · 独&emsp;$$21 · · 彷&emsp;$|$6 &emsp; 徨$ – – – |$2 · 今$ $2 · 宵$ $1 ·$ $63 &emsp; 入&emsp;$$16 ·&emsp; 梦&emsp;$|$5 &emsp; 乡$ – – – ||$2 &emsp; 今$32 12 $3 &emsp; 日$ 5 |$51 &emsp;· 花&emsp;$$65 &emsp; 落&emsp;$ $6 &emsp; 水$ $3 · 今$$23 · ·$|$2 · 晚$• $1 ·$ $5 &emsp; 海$ $6 &emsp; 盈$56 |$1 · 光$ – – – |

Gauss's algorithm
In a handwritten note in a collection of astronomical tables, Carl Friedrich Gauss described a method for calculating the day of the week for 1 January in any given year. He never published it. It was finally included in his collected works in 1927.

Gauss' method was applicable to the Gregorian calendar. He numbered the weekdays from 0 to 6 starting with Sunday. He defined the following operation: The weekday of 1 January in year number $A$ is
 * $$ R(1 + 5R(A-1,4) + 4R(A-1,100) + 6R(A-1,400),7)$$

or
 * $$ R(1 + 5R(Y-1,4) + 3(Y-1) + 5R(C,4),7)$$

from which a method for the Julian calendar can be derived
 * $$ R(6 + 5R(A-1,4) + 3(A-1),7)$$

or
 * $$ R(6 + 5R(Y-1,4) + 3(Y-1) + 6C,7)$$

where $R(y,m)$ is the remainder after division of $y$ by $m$, or $y$ modulo $m$, $Y$ = $R$($A$, 100) and $C$ = ($A$ - $Y$)/100.

For year number 2000, $A$ - 1 = 1999, $Y$ - 1 = 99 and $C$ = 19, the weekday of 1 January is
 * $$\begin{align}&= R(1 + 5R(1999,4) + 4R(1999,100) + 6R(1999,400),7)\\ &= R(1 + 1 + 4 + 0),7)\\ & =6\end{align} = Saturday$$
 * $$\begin{align}&= R(1 + 5R(99,4) + 3\times 99 + 5R(19,4),7)\\ &= R(1 + 1 + 3 + 1,7)\\ &= 6 =Saturday.\end{align} $$

The weekday of the last day in year number $A$ - 1 or 0 January in year number $A$ is
 * $$ R(5R(A-1,4) + 4R(A-1,100) + 6R(A-1,400),7)$$

The weekday of 0 (a common year) or 1 (a leap year) January in year number $A$ is
 * $$ R(5R(A,4) + 4R(A,100) + 6R(A,400) - 1,7)$$

Note: minus 1 if $m$ is 11 or 12 and plus 1 if $M$ less than 11 in a leap year.

The day of the week for any day in year mumber A is
 * $$ R(D + m + 5R(A-1,4) + 4R(A-1,100) + 6R(A-1,400),7)$$

or
 * $$ R(D + \left\lceil 2.6M\right\rceil + 5R(A,4) + 4R(A,100) + 6R(A,400) - 1,7)$$

where $M$ is the day of the month and $D$ - 1 for Jan or Feb.

The weekdays for 30 April 1777 and 23 February 1855 are
 * $$\begin{align}&= R(30 + 6 + 5R(1776,4) + 4R(1776,100) + 6R(1776,400),7)\\ &= R(2 + 6 + 0 + 3 + 6,7)\\&=3=Tuesday\end{align} $$

and
 * $$ \begin{align}&= R(23 + \left\lceil 2.6\times 12\right\rceil + 5R(1854,4) + 4R(1854,100) + 6R(1854,400) - 1,7)\\ &= R(2 + 4 + 3 + 6 + 5 - 1,7)\\ &= 5=Friday.\end{align} $$

This formula was also converted into graphical and tabular methods for calculating any day of the week by Kraitchik and Schwerdtfeger.

Disparate variation
Another variation of the above algorithm likewise works with no lookup tables. A slight disadvantage is the unusual month and year counting convention. The formula is


 * $$w = \left(d + \lfloor 2.6m - 0.2 \rfloor + y + \left\lfloor\frac{y}{4}\right\rfloor + \left\lfloor\frac{c}{4}\right\rfloor - 2c\right) \bmod 7,$$

where
 * $A$ is the year minus 1 for January or February, and the year for any other month
 * $Y$ is the last 2 digits of Y
 * $y$ is the first 2 digits of Y
 * $c$ is the day of the month (1 to 31)
 * $d$ is the shifted month (March=1,...,February=12)
 * $m$ is the day of week (0=Sunday,...,6=Saturday). If w is negative you have to add 7 to it.

For example, January 1, 2000. (year − 1 for January)


 * $$\begin{align} w &= \left(1 + \lfloor 2.6 \cdot 11 - 0.2 \rfloor + (0 - 1) + \left\lfloor\frac{0 - 1}{4}\right\rfloor + \left\lfloor\frac{20}{4}\right\rfloor - 2 \cdot 20\right) \bmod 7 \\ &= (1 + 28 - 1 -1 + 5 - 40) \bmod 7 \\ &= 6 = \text{Saturday} \end{align}$$


 * $$\begin{align} w &= \left(1 + \lfloor 2.6 \cdot 11 - 0.2 \rfloor + (100 - 1) + \left\lfloor\frac{100 - 1}{4}\right\rfloor + \left\lfloor\frac{20 - 1}{4}\right\rfloor - 2 \cdot (20 - 1)\right) \bmod 7 \\ &= (1 + 28 +99 + 24 + 4 - 38) \bmod 7 \\ &= 6 = \text{Saturday} \end{align}$$

Note: The first is only for a 00 leap year and the second is for any 00 years.

The term $⌊2.6m − 0.2⌋$ mod 7 gives the values of months: $w$

The term $y + ⌊y/4⌋$ mod 7 gives the values of years: $m$

The term $⌊c/4⌋ − 2c$ mod 7 gives the values of centuries: $m$

Now from the general formula: $$w = d + m + y +c \bmod 7$$; January 1, 2000 can be recalculated as follows:

$$\begin{align} w &= 1 + 0 + 5 + 0 \bmod 7 = 6 = \text{Saturday}\\ d &= 1, m = 0\\ y &= 5 (0 - 1 \bmod 28 = 27)\\ c &= 0 (20 \bmod 4 = 0) \end{align}$$

$$\begin{align} w &= 1 + 0 + 4 + 1 \bmod 7 = 6 = \text{Saturday}\\ d &= 1, m = 0\\ y &= 4 (99 \bmod 28 = 15)\\ c &= 1 (20 - 1 \bmod 4 = 3) \end{align}$$

Lewis Carroll's method
Algorithm: Take the given date in 4 portions, viz. the number of centuries, the number of years over, the month, the day of the month. Compute the following 4 items, adding each, when found, to the total of the previous items. When an item or total exceeds 7, divide by 7, and keep the remainder only.

Century-item: For `Old Style' (which ended 2 September 1752) subtract from 18. For `New Style' (which began 14 September 1752) divide by 4, take overplus from 3, multiply remainder by 2.
 * $$ \begin{align}\text{Century-item NS} & = 2(3 - c\bmod 4) \\ & = 6 - 2(c\bmod 4) \\ & = \left(\left\lfloor\frac{c}{4}\right\rfloor - 2c - 1\right) \bmod 7 \\ & = \text{6 4 2 0} \\ \text{OS} & = (18 - c ) \bmod 7 \\ & = (5 - c - 1) \bmod 7 \\ & = \text{4 3 2 1 0 6 5}.\end{align}$$

Year-item: Add together the number of dozens, the overplus, and the number of 4s in the overplus.
 * $$ \begin{align}\text{Year-item} & = \left(n + y -

12n +\left\lfloor\frac{y-12n}{4}\right\rfloor\right) \bmod 7 \\ & =\left(n + y - 5n + \left\lfloor\frac{y}{4}\right\rfloor - 3n\right) \bmod 7 \\ & =\left(y + \left\lfloor\frac{y}{4}\right\rfloor\right) \bmod 7\\ \end{align}$$ Month-item: If it begins or ends with a vowel, subtract the number, denoting its place in the year, from 10. This, plus its number of days, gives the item for the following month. The item for January is "0"; for February or March, "3"; for December, "12".
 * $$ \begin{align}\text{Month-item} & = \text{Jan 0(6) 3(2) 3 6 1 4 6 2 5 0 3 5 Dec}\\ & \text{January is the first month of the year.}\\ \text{Month-item} & = \left\lceil\frac{13m}{5}\right\rceil\bmod 7 \text{ (modified)}\\ & =\text{(1 4) Mar 3 6 1 4 6 2 5 0 3 5 1 4 Feb} \\ & \text{March is the first month of the year.} \end{align}$$

Day-item: The total, thus reached, must be corrected, by deducting "1" (first adding 7, if the total be "0"), if the date be January or February in a leap year, remembering that every year, divisible by 4, is a Leap Year, excepting only the century-years, in `New Style', when the number of centuries is not so divisible (e.g. 1800).
 * $$ \begin{align}\text{Day-item} = d\bmod 7. \end{align}$$

The final result gives the day of the week, "0" meaning Sunday, "1" Monday, and so on.
 * $$ \begin{align}\text{The total} & = (\text{Century-item + Year-item + Month-item + Day-item})\bmod 7 \\ & = \text{0 1 2 3 4 5 6} \\ & = \text{Sun Mon Tue Wed Thu Fri Sat.}\end{align}$$

Examples: 17, divided by 4, leaves "1" over; 1 from 3 gives "2"; twice 2 is "4". 83 is 6 dozen and 11, giving 17; plus 2 gives 19, i.e. (dividing by 7) "5". Total 9, i.e. "2" The item for August is "8 from 10", i.e. "2"; so, for September, it is "2 plus 31", i.e. "5" Total 7, i.e. "0", which goes out. 18 gives "4". Answer, "Thursday". $$\begin{align}c & = 17 = 16 + 1\\ C & = (3 - 1)\times 2 = 4\end{align}$$
 * 1783, September 18

$$\begin{align} y & = 6\times 12 + 2\times 4 + 3\\ Y & = (6 + 11 + 2)\bmod 7 = 5\end{align}$$

$$\begin{align} m & = 7\\ M & = \left\lceil 2.6\times 7\right\rceil\bmod 7 = 5\end{align}$$

$$\begin{align} d & = 18 = 14 + 4\\ D & = 4\end{align}$$

$$\begin{align} T & = (4 + 5 + 5 + 4)\bmod 7 = 4\\ W & = Thu\end{align}$$ 16 from 18 gives "2" 76 is 6 dozen and 4, giving 10; plus 1 gives 11, i.e. "4". Total "6" The item for February is "3". Total 9, i.e. "2" 23 gives "2". Total "4" Correction for Leap Year gives "3". Answer, "Wednesday". $$\begin{align} c & = 16\\ C & = 18 - 16 = 2\end{align}$$
 * 1676, February 23

$$\begin{align} y & = 76 = 6\times 12 + 1\times 4\\ Y & = (6 + 4 + 1)\bmod 7 = 4\end{align}$$

$$\begin{align} m & = 12\\ M & = \left\lceil 2.6\times 12\right\rceil\bmod 7 = 4\end{align}$$

$$\begin{align} d & = 23 = 21 + 2\\ D & = 2\end{align}$$

$$\begin{align} T & = (2 + 4 + 4 + 2)\bmod 7 = 5\\ W & = Fri\end{align}$$

Table
每年的一月和二月都有四天的週數是固定的. 除外以星期四開始的閏年(三月份起週數加一)，每個月都有四到五天的週數是固定的(詳見下表).

Table 1
主日字母表不僅可以用來查找格里曆(CD)任意一年的主日字母(DL)和任意一天的日字母(dl)及星期(w)，而卻可以查找ISO周日曆(WD)的週數(n)及其相應的日期(D)，因此可以利用該表來實現這兩種日曆的相互轉換.

位於世紀(c)列和年(y)行交叉處(c, y)的字母就是該年的主日字母(本世紀的主日字母位於A列)，而位於日列(d)和月(m)行交叉點(d, m)的字母就是該日的字母，知道了主日字母就可以確定其它日字母的星期. 由字母D就可以查到週數固定的日期，當遇到以週四開始的閏年(DC)從三月份起週數加一.

例一查找2032年10月1日的星期及週數:
 * c = 20，y = 32 mod 28 = 4，d = 1，m = 10；
 * DL = ( 20 ， 04 / 04 ) = DC，dl = (1，10) = A，D = (4，10)(40 + 1)；
 * C = 週日，A = 週五，D = 週一(41)；
 * n = 41 - 1 = 40，w = 5；
 * WD = 2032–W40–5.

例二查找1980–W40–1的日期:
 * c = 19，y = 80 mod 28 = 24，n = 40，w = 1 = 週一；
 * DL = (19，24/ 24 ) = FE，D = (4，10)；
 * E = 週日，D = 週六(40)，F = (6，10)週一(41) = (-1，10)(29，9)週一(40)；
 * CD = 1980年9月29日週一.

This table can be used to look up dominical letters (DL), day letters (dl), weekdays (w), week numbers (n), and week dates (W). Letters both in a century column ( A C E G ) and year rows are dominical letters for years of the century (c, y). Letters both in day columns and a month row are day letters for days of the month (d, m).
 * For 1 October 2032
 * c = 20, y = 32 mod 28 = 4, d = 1, m = Oct;
 * DL = ( 20, 04 / 04 ) = DC, dl = (1, Oct) = A, D = 4 Oct;
 * C = Sun, A = Fri = 5, D = Mon;
 * n = 40 + 1 - 1 = 40, w = 5;
 * W = 2032–W40–5.


 * For 1980–W40–1
 * c = 19, y = 80 mod 28 = 24, n = 40, w = 1 = Mon;
 * DL = (19, 24/ 24 ) = FE, m = Oct, D = 4;
 * E = Sun, D = Sat = 6;
 * d = 4 + 1 - 6 = -1 Oct = 29 September 1980.

Table 2

 * $$\begin{align}

\text{weeks}(\text{year}) &= 52 + \begin{cases} 1 \text{ (long)} & \text{if } p(\text{year}) = 4 \\ & \text{or } p(\text{year}-1) = 3 \\ 0 \text{ (short)} & \text{otherwise} \end{cases} \\ p(\text{year}) &= \left( \text{year} + \left\lfloor\frac{\text{year}}4\right\rfloor - \left\lfloor\frac{\text{year}}{100}\right\rfloor + \left\lfloor\frac{\text{year}}{400} \right\rfloor \right) \bmod 7\end{align}$$


 * $$\begin{align}

\text{Week date}\begin{cases}\text{week number} &= \left\lceil\frac{od + c}{7}\right\rceil\\ \text{weekday number} &= (od + c)\bmod 7\end{cases}\end{align}$$ for od and c look up the table above or use the algorithm to calculate. There are 53 weeks in any year (c = 3) or in leap years (c = 2), otherwise there are 52 weeks in a year.

Ceiling the quotient equals the week number and the remainder is the weekday number (0 = Sunday = 7).

For 26 September 2008
 * ceil ((244 + 26 + 01 )/7) = 39
 * (244 + 26 + 01 ) mod 7 = 5

the week date is 2008W395.