Doomsday rule
- About the task
John Conway (1937-2020), was a mathemetician who also invented several mathematically oriented computer pastimes, such as the famous Game of Life cellular automaton program. Dr. Conway invented a simple algorithm for finding the day of the week, given any date. The algorithm was based opon calculating the distance of a given date from certain "anchor days" which follow a pattern for the day of the week upon which they fall.
- Algorithm
The formula is calculated assuming that Sunday is 0, Monday 1, and so forth with Saturday 7, and
anchorday = (Tuesday(or 2) + 5(y mod 4) + 4(y mod 100) + 6(y mod 400)) % 7
which, for 2021, is 0 (Sunday).
To calculate the day of the week, we then count days from a close doomsday, with these as charted here by month, then add the doomsday for the year, then get the remainder after divding by 7. This should give us the number corresponding to the day of the week for that date.
Month Doomsday Dates for Month -------------------------------------------- January (common years) 3, 10, 17, 24, 31 January (leap years) 4, 11, 18, 25 February (common years) 7, 14, 21, 28 February (leap years) 1, 8, 15, 22, 29 March 7, 14, 21, 28 April 4, 11, 18, 25 May 2, 9, 16, 23, 30 June 6, 13, 20, 27 July 4, 11, 18, 25 August 1, 8, 15, 22, 29 September 5, 12, 19, 26 October 3, 10, 17, 24, 31 November 7, 14, 21, 28 December 5, 12, 19, 26
- Task
Given the following dates:
- 1800-01-06 (January 6, 1800)
- 1875-03-29 (March 29, 1875)
- 1915-12-07 (December 7, 1915)
- 1970-12-23 (December 23, 1970)
- 2043-05-14 (May 14, 2043)
- 2077-02-12 (February 12, 2077)
- 2101-04-02 (April 2, 2101)
Use Conway's Doomsday rule to calculate the day of the week for each date.
- see also
BASIC
Note that 1/6/1800 is actually a Monday, not a Sunday. As far as I can tell this is actually the correct day.
<lang BASIC>10 DIM D$(7): FOR I=1 TO 7: READ D$(I): NEXT I 20 DIM D(12,1): FOR I=0 TO 1: FOR J=1 TO 12: READ D(J,I): NEXT J,I 30 READ Y: IF Y=0 THEN END ELSE READ M,D 40 PRINT USING "##/##/####: ";M;D;Y; 50 C=Y\100: R=Y MOD 100 60 S=R\12: T=R MOD 12 70 A=(5*(C AND 3)+2) MOD 7 80 B=(S+T+(T\4)+A) MOD 7 90 PRINT D$((B+D-D(M,-(Y MOD 4=0 AND (Y MOD 100<>0 OR Y MOD 400=0)))+7) MOD 7+1) 100 GOTO 30 110 DATA Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday 120 DATA 3,7,7,4,2,6,4,1,5,3,7,5 130 DATA 4,1,7,4,2,6,4,1,5,3,7,5 140 DATA 1800,1,6, 1875,3,29, 1915,12,7, 1970,12,23 150 DATA 2043,5,14, 2077,2,12, 2101,4,2, 0</lang>
- Output:
1/ 6/1800: Monday 3/29/1875: Monday 12/ 7/1915: Tuesday 12/23/1970: Wednesday 5/14/2043: Thursday 2/12/2077: Friday 4/ 2/2101: Saturday
BCPL
Note that 1/6/1800 is actually a Monday, not a Sunday. As far as I can tell this is actually the correct day.
<lang bcpl>get "libhdr"
let dayname(n) =
n = 0 -> "Sunday", n = 1 -> "Monday", n = 2 -> "Tuesday", n = 3 -> "Wednesday", n = 4 -> "Thursday", n = 5 -> "Friday", n = 6 -> "Saturday", dayname(n rem 7)
let leap(year) = year rem 4 = 0 & (year rem 100 ~= 0 | year rem 400 = 0)
let weekday(y, m, d) = valof $( let leapdoom = table 4,1,7,4,2,6,4,1,5,3,7,5
let normdoom = table 3,7,7,4,2,6,4,1,5,3,7,5
let c = y / 100 and r = y rem 100 let s = r / 12 and t = r rem 12 let an = (5 * (c rem 4) + 2) rem 7 let doom = (s + t + (t/4) + an) rem 7 let anchor = (leap(y) -> leapdoom, normdoom)!(m-1) resultis (doom + d - anchor + 7) rem 7
$)
let start() be $( writef("January 6, 1800 was on a %S.*N", dayname(weekday(1800, 1, 6)))
writef("March 29, 1875 was on a %S.*N", dayname(weekday(1875, 3, 29))) writef("December 7, 1915 was on a %S.*N", dayname(weekday(1915, 12, 7))) writef("December 23, 1970 was on a %S.*N", dayname(weekday(1970, 12, 23))) writef("May 14, 2043 will be on a %S.*N", dayname(weekday(2043, 5, 14))) writef("February 12, 2077 will be on a %S.*N", dayname(weekday(2077, 2, 12))) writef("April 2, 2101 will be on a %S.*N", dayname(weekday(2101, 4, 2)))
$)</lang>
- Output:
January 6, 1800 was on a Monday. March 29, 1875 was on a Monday. December 7, 1915 was on a Tuesday. December 23, 1970 was on a Wednesday. May 14, 2043 will be on a Thursday. February 12, 2077 will be on a Friday. April 2, 2101 will be on a Saturday.
Julia
<lang julia>module DoomsdayRule export get_weekday
const weekdaynames = ["Sunday", "Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
- For months 1 through 12, the date of the first doomsday that month.
const leapyear_firstdoomsdays = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] const nonleapyear_firstdoomsdays = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
"""
get_weekday(year::Int, month::Int, day::Int)::String
Return the weekday of a given date in past or future. Uses Conway's doomsday rule (see also https://en.wikipedia.org/wiki/Doomsday_rule) """ function get_weekday(year::Int, month::Int, day::Int)::String
# sanity checks @assert 1582 <= year <= 9999 "Invalid year (should be after 1581 and 4 digits)" @assert 1 <= month <= 12 "Invalid month, should be between 1 and 12" @assert 1 <= day <= 31 "Invalid day, should be between 1 and 31"
# Conway's doomsday algorithm c, r = divrem(year, 100) s, t = divrem(r, 12) canchor = (5 * (c % 4) + 2) % 7 # anchor for centiry doomsday = (s + t + (t ÷ 4) + canchor) % 7 anchorday = (year % 4 != 0) || (r == 0 && year % 400 == 0) ? # leap year determination nonleapyear_firstdoomsdays[month] : leapyear_firstdoomsdays[month] weekday = (doomsday + day - anchorday + 7) % 7 + 1 return weekdaynames[weekday]
end
end # module
using .DoomsdayRule
println("January 6, 1800 was on a ", get_weekday(1800, 1, 6)) println("March 29, 1875 was on a ", get_weekday(1875, 3, 29)) println("December 7, 1915 was on a ", get_weekday(1915, 12, 7)) println("December 23, 1970 was on a ", get_weekday(1970, 12, 23)) println("May 14, 2043 will be on a ", get_weekday(2043, 5, 14)) println("February 12, 2077 will be on a ", get_weekday(2077, 2, 12)) println("April 2, 2101 will be on a ", get_weekday(2101, 4, 2))
</lang>
- Output:
January 6, 1800 was on a Sunday March 29, 1875 was on a Monday December 7, 1915 was on a Tuesday December 23, 1970 was on a Wednesday May 14, 2043 will be on a Thursday February 12, 2077 will be on a Friday April 2, 2101 will be on a Saturday
Wren
We only use the above module to check the dates of the week given by Conway's method. The latter are worked out from scratch. <lang ecmascript>import "/date" for Date
var days = ["Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday"]
var anchorDay = Fn.new { |y| (2 + 5 * (y%4) + 4 *(y%100) + 6 * (y%400)) % 7 }
var isLeapYear = Fn.new { |y| y%4 == 0 && (y%100 != 0 || y%400 == 0) }
var firstDaysCommon = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] var firstDaysLeap = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
var dates = [
"1800-01-06", "1875-03-29", "1915-12-07", "1970-12-23", "2043-05-14", "2077-02-12", "2101-04-02"
]
System.print("Days of week given by Doomsday rule:") for (date in dates) {
var y = Num.fromString(date[0..3]) var m = Num.fromString(date[5..6]) - 1 var d = Num.fromString(date[8..9]) var a = anchorDay.call(y) var w = d - (isLeapYear.call(y) ? firstDaysLeap[m] : firstDaysCommon[m]) if (w < 0) w = 7 + w var dow = (a + w) % 7 System.print("%(date) -> %(days[dow])")
}
System.print("\nDays of week given by Date module:") for (date in dates) {
var d = Date.parse(date, Date.isoDate) System.print("%(date) -> %(d.weekDay)")
}</lang>
- Output:
Days of week given by Doomsday rule: 1800-01-06 -> Monday 1875-03-29 -> Monday 1915-12-07 -> Tuesday 1970-12-23 -> Wednesday 2043-05-14 -> Thursday 2077-02-12 -> Friday 2101-04-02 -> Saturday Days of week given by Date module: 1800-01-06 -> Monday 1875-03-29 -> Monday 1915-12-07 -> Tuesday 1970-12-23 -> Wednesday 2043-05-14 -> Thursday 2077-02-12 -> Friday 2101-04-02 -> Saturday