Calendar arithmetic is the use of integer arithmetic and modular arithmetic to compute elapsed days, weekdays, and recurring date patterns. Assigning consecutive integers to consecutive dates reduces elapsed-day calculations to subtraction. Converting between calendars can similarly be reduced to converting to and from a common day number (Dershowitz and Reingold 1990, pp. 900-901).
For a concrete construction, apply the Gregorian rules to every positive integer year .
A year is a leap year iff
is a multiple of 4 and is either
not a multiple of 100 or is a multiple
of 400. Let
be the length of month
. The lengths from January through December are 31, 28, 31,
30, 31, 30, 31, 31, 30, 31, 30, 31, with February increased to 29 days in a leap
year. Number January 1 of year 1 as day 1. A valid date with month
and day
then has day number
|
(1)
|
where ,
,
and
is the floor function (Dershowitz and Reingold
1990, pp. 903-905). The combination of the three floor
function terms counts leap days in preceding years. For an integer
with
, adding
days changes the day number to
. The inverse calculation finds the year and month containing
the day number, then obtains the day by subtraction
(Dershowitz and Reingold 1990, pp. 905-906).
With Sunday through Saturday encoded by 0 through 6, the weekday is , where mod gives the remainder
from 0 through 6 and day 1 is a Monday (Dershowitz and Reingold 1990, p. 901).
Thus two dates have the same weekday iff their day numbers
are congruent modulo 7. The doomsday
algorithm is a mental method for the weekday calculation that uses selected dates
with a common weekday as anchors. For a month-day that exists in consecutive years,
the weekday advances by one or two days according as the interval contains 365 or
366 days. The two-day shift occurs when the interval includes February 29. February
29 itself has no occurrence in the intervening common years.
Every block of 400 consecutive years contains leap years, so
|
(2)
|
The month-length pattern therefore repeats with the same weekdays after 400 years. Among the 4800 months in a complete cycle, the numbers whose thirteenth day falls
on Sunday through Saturday are 687, 685, 685, 687, 684, 688, 684 (OEIS A101944).
Consequently, a month selected uniformly from the cycle has a Friday the thirteenth
with probability , slightly greater than
.
The birthday problem requires an additional sampling model. If days are sampled independently and uniformly from a complete 400-year cycle, each month-day other than February 29 occurs 400 times, while February 29 occurs 97 times. The probability that two such samples have the same month and day is therefore
|
(3)
|
This model gives unequal probabilities to the 366 possible month-day labels. Including February 29 therefore does not by itself justify a discrete uniform distribution on 366 birthdays.