How to resolve the algorithm Doomsday rule step by step in the uBasic/4tH programming language
How to resolve the algorithm Doomsday rule step by step in the uBasic/4tH 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 uBasic/4tH programming language
Source code in the ubasic/4th programming language
Dim @f(24)
Push 3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5 ' the first doomsday in each
Push 4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5 ' month for common and leap years
For x = 23 to 0 Step -1 : @f(x) = Pop() : Next
Dim @d(7)
Push "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
For x = 6 To 0 Step -1 : @d(x) = Pop() : Next
Print Show(FUNC(_get_day(1800, 01, 06)))
Print Show(FUNC(_get_day(1875, 03, 29)))
Print Show(FUNC(_get_day(1915, 12, 07)))
Print Show(FUNC(_get_day(1970, 12, 23)))
Print Show(FUNC(_get_day(2043, 05, 14)))
Print Show(FUNC(_get_day(2077, 02, 12)))
Print Show(FUNC(_get_day(2101, 04, 02)))
End
_doomsday ' John Conway's doomsday formula
Param (1) : Return ((2 + 5*(a@ % 4) + 4*(a@ % 100) + 6*(a@ % 400)) % 7)
_leap ' is it a leap year?
Param (1)
If (a@ % 4 > 0) Then Return (0) ' return 0 for common years
If (a@ % 100 = 0) * (a@ % 400 > 0) Then Return (0)
Return (1) ' 1 for leap years
_get_day
Param (3)
Local (2)
d@ = FUNC(_doomsday(a@))
e@ = (7 + c@ - @f(FUNC(_leap(a@)) * 2 + (b@-1))) % 7
Return (@d((d@+e@) % 7))
You may also check:How to resolve the algorithm Add a variable to a class instance at runtime step by step in the LOLCODE programming language
You may also check:How to resolve the algorithm Rot-13 step by step in the Ruby programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Ruby programming language
You may also check:How to resolve the algorithm Barnsley fern step by step in the Oberon-2 programming language
You may also check:How to resolve the algorithm Abundant odd numbers step by step in the ARM Assembly programming language