Jump to content

Doomsday rule: Difference between revisions

no edit summary
mNo edit summary
No edit summary
Line 57:
*   [[wp:Doomsday rule|Doomsday rule]]
*   [https://www.archim.org.uk/eureka/archive/Eureka-36.pdf Tomorrow is the Day After Doomsday (p.28)]
 
 
 
 
=={{header|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)
canchor = (5 * (c % 4) + 2) % 7 # anchor for centiry
doomsday = ((r ÷ 12) + (r % 12) + ((r % 12) ÷ 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>{{out}}
<pre>
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
</pre>
4,108

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.