How to resolve the algorithm Doomsday rule step by step in the Cowgol programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Doomsday rule step by step in the Cowgol 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 Cowgol programming language

Source code in the cowgol programming language

include "cowgol.coh";

record Date is
    year: uint16;
    month: uint8;
    day: uint8;
end record;

sub weekday(d: [Date]): (dayname: [uint8]) is
    var daynames: [uint8][] := {
        "Sunday", "Monday", "Tuesday", "Wednesday", 
        "Thursday", "Friday", "Saturday"
    };
    
    var leapdoom: uint8[] := {4,7,1,4,2,6,4,1,5,3,7,5};
    var normdoom: uint8[] := {3,7,7,4,2,6,4,1,5,3,7,5};
    
    var c := d.year / 100;
    var r := d.year % 100;
    var s := r / 12;
    var t := r % 12;
    
    var can := (5 * (c%4) + 2) % 7;
    var doom := (s + t + (t/4) + can) % 7;
    var anchor: int16;
    if d.year%4 == 0 and (d.year%100 != 0 or d.year%400 == 0) then
        anchor := leapdoom[d.month-1] as int16;
    else
        anchor := normdoom[d.month-1] as int16;
    end if;
    
    var dayno := (doom as int16 + d.day as int16 - anchor + 7) % 7;
    dayname := daynames[dayno as @indexof daynames];
end sub;

sub print_date(d: [Date]) is
    print_i8(d.month); print_char('/');
    print_i8(d.day); print_char('/');
    print_i16(d.year); print(": ");
    print(weekday(d));
    print_nl();
end sub;

var dates: Date[] := {
    {1800,1,6}, {1875,3,29}, {1915,12,7}, {1970,12,23}, {2043,5,14},
    {2077,2,12}, {2101,4,2}
};

var i: @indexof dates := 0;
while i < @sizeof dates loop
    print_date(&dates[i]);
    i := i + 1;
end loop;

  

You may also check:How to resolve the algorithm Comments step by step in the AmigaE programming language
You may also check:How to resolve the algorithm Regular expressions step by step in the MUMPS programming language
You may also check:How to resolve the algorithm Collections step by step in the Ursala programming language
You may also check:How to resolve the algorithm Comments step by step in the Fancy programming language
You may also check:How to resolve the algorithm Yin and yang step by step in the Wren programming language