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

Published on 12 May 2024 09:40 PM

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

Source code in the unix programming language

#!/usr/bin/env bash

day-of-the-week()
  if [[ "$1" =~ ([0-9]{4})-([0-9]{2})-([0-9]{2}) ]]
  then
    local -ra names=({Sun,Mon,Tues,Wednes,Thurs,Fri,Satur}day) doomsday=({37,41}7426415375)
    local -i i c s t a b
    local -i {year,month,day}=${BASH_REMATCH[++i]}
    echo ${names[
      c=year/100,
      s=(year%100)/12,
      t=(year % 100) % 12,
      a=(5*(c%4)+2) % 7,
      b=(s + t + (t / 4) + a ) % 7,
      (b + day - ${doomsday[(year%4 == 0) && ((year%100) || (year%400 == 0))]:month-1:1} + 7) % 7
    ]}
  else return 1
  fi

for date in 1800-01-06 1875-03-29 1915-12-07 1970-12-23 2043-05-14 2077-02-12 2101-04-02 
do day-of-the-week "$date"
done


  

You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Order programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the ALGOL 60 programming language
You may also check:How to resolve the algorithm Dutch national flag problem step by step in the ZX Spectrum Basic programming language
You may also check:How to resolve the algorithm Fibonacci word step by step in the Python programming language