How to resolve the algorithm Doomsday rule step by step in the FreeBASIC programming language
How to resolve the algorithm Doomsday rule step by step in the FreeBASIC programming language
Table of Contents
Problem Statement
John Conway (1937-2020), was a mathematician 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 on 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. The formula is calculated assuming that Sunday is 0, Monday 1, and so forth with Saturday 7, and 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 dividing by 7. This should give us the number corresponding to the day of the week for that date. Given the following dates:
Use Conway's Doomsday rule to calculate the day of the week for each date.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Doomsday rule step by step in the FreeBASIC programming language
Source code in the freebasic programming language
dim shared as ubyte fdoom(0 to 1, 1 to 12) = {_
{ 3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5 }, _
{ 4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5 } } 'the first doomsday in
'each month for common
'and leap years
dim shared as string*10 days(0 to 6) = {"Sunday", "Monday", "Tuesday", "Wednesday",_
"Thursday", "Friday", "Saturday"}
function doomsday(y as uinteger) as ubyte
' John Conway's doomsday formula
return (2 + 5*(y mod 4) + 4*(y mod 100) + 6*(y mod 400)) mod 7
end function
function leap(y as uinteger) as ubyte
'is it a leap year?
'return 0 for common years, 1 for leap years
if y mod 4 > 0 then return 0
if y mod 100 = 0 and y mod 400 > 0 then return 0
return 1
end function
function get_day(y as uinteger, m as ubyte, d as ubyte) as string
dim as ubyte c = doomsday(y), diff
diff = (7 + d - fdoom( leap(y), m )) mod 7
return days( (c+diff) mod 7 )
end function
print get_day( 1800, 01, 06 )
print get_day( 1875, 03, 29 )
print get_day( 1915, 12, 07 )
print get_day( 1970, 12, 23 )
print get_day( 2043, 05, 14 )
print get_day( 2077, 02, 12 )
print get_day( 2101, 04, 02 )
You may also check:How to resolve the algorithm Monads/Writer monad step by step in the Factor programming language
You may also check:How to resolve the algorithm HTTP step by step in the Ring programming language
You may also check:How to resolve the algorithm Law of cosines - triples step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Count in octal step by step in the Lang5 programming language
You may also check:How to resolve the algorithm Digital root step by step in the 11l programming language