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

Published on 12 May 2024 09:40 PM

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

Source code in the clu programming language

leap_year = proc (year: int) returns (bool)
    return(year//4=0 & (year//100=0 | year//400=0))
end leap_year

weekday = proc (d: date) returns (string)
    own leapdoom: array[int] := array[int]$[4,1,7,2,4,6,4,1,5,3,7,5]
    own normdoom: array[int] := array[int]$[3,7,7,4,2,6,4,1,5,3,7,5]
    own days: array[string] := array[string]$[0:
        "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
        "Friday", "Saturday"
    ]
    
    c: int := d.year/100
    r: int := d.year//100
    s: int := r/12
    t: int := r//12
    
    c_anchor: int := (5 * (c//4) + 2) // 7
    doom: int := (s + t + t/4 + c_anchor) // 7
    anchor: int
    if leap_year(d.year) 
        then anchor := leapdoom[d.month]
        else anchor := normdoom[d.month]
    end
    return(days[(doom+d.day-anchor+7)//7])
end weekday

start_up = proc ()
    po: stream := stream$primary_output()
    dates: array[date] := array[date]$
       [date$create( 1, 6,1800,0,0,0),
        date$create(29, 3,1875,0,0,0),
        date$create( 7,12,1915,0,0,0),
        date$create(23,12,1970,0,0,0),
        date$create(14, 5,2043,0,0,0),
        date$create(12, 2,2077,0,0,0),
        date$create( 2, 4,2101,0,0,0)]
    
    for d: date in array[date]$elements(dates) do
        stream$puts(po, date$unparse_date(d))
        if d
            then stream$puts(po, " was on a ")
            else stream$puts(po, " will be on a ")
        end
        stream$putl(po, weekday(d))
    end
end start_up

  

You may also check:How to resolve the algorithm Create an HTML table step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm URL encoding step by step in the Rust programming language
You may also check:How to resolve the algorithm Non-continuous subsequences step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Palindromic gapful numbers step by step in the Nim programming language