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

Published on 12 May 2024 09:40 PM

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

Source code in the applescript programming language

on dayOfWeek(yyyymmdd)
    tell yyyymmdd to set {y, m, d} to {word 1 as integer, word 2 as integer, word 3 as integer}
    set doomsdayForYear to (y + y div 4 - y div 100 + y div 400 + 2) -- (mod 7 further down anyway)
    if ((m < 3) and ((y mod 4 = 0) and (y mod 100 > 0) or (y mod 400 = 0))) then set m to m + 12
    set doomsdayInMonth to item m of {3, 28, 7, 4, 2, 6, 4, 8, 5, 10, 7, 12, 4, 29}
    
    return item ((doomsdayForYear + d - doomsdayInMonth + 28) mod 7 + 1) of ¬
        {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
end dayOfWeek

on join(lst, delim)
    set astid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to delim
    set txt to lst as text
    set AppleScript's text item delimiters to astid
    return txt
end join

on task()
    set dateStrings to {"1800-01-06", "1875-03-29", "1915-12-07", "1970-12-23", "2043-05-14", "2077-02-12", "2101-04-02"}
    set output to {}
    repeat with yyyymmdd in dateStrings
        set end of output to yyyymmdd & " --> " & dayOfWeek(yyyymmdd)
    end repeat
    return join(output, linefeed)
end task

task()


"1800-01-06 --> Monday
1875-03-29 --> Monday
1915-12-07 --> Tuesday
1970-12-23 --> Wednesday
2043-05-14 --> Thursday
2077-02-12 --> Friday
2101-04-02 --> Saturday"


  

You may also check:How to resolve the algorithm Tarjan step by step in the zkl programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Falcon programming language
You may also check:How to resolve the algorithm EKG sequence convergence step by step in the Wren programming language
You may also check:How to resolve the algorithm Count in factors step by step in the Maple programming language
You may also check:How to resolve the algorithm Draw a sphere step by step in the Turing programming language