Program to calculate Dooms Day for a year


The doomsday is also known as the doomsday of the week which is a specific day of the week that falls on the same date every year. The concept of the doomsday is based on the doomsday algorithm, which allows us to determine the day of the week for any given date.

The doomsday algorithm was developed by mathematician John Horton Conway and is based on the idea that certain dates within each year fall on the same day of the week called the doomsday. The doomsday occurs on the following dates −

  • January 3rd

  • February 7th or 14th in leap years

  • March 7th

  • April 4th

  • May 9th

  • June 6th

  • July 11th

  • August 8th

  • September 5th

  • October 10th

  • November 7th

  • December 12th

The following are the steps to be followed to calculate the dooms day.

Determining the century anchor day

The anchor day refers to a specific day of the week that serves as a reference point for calculating the day of the week for any given date. It acts as a starting point or an anchor for the calculations involved in determining the day of the week.

To calculate the century anchor day, the year is divided by 100 to determine the century. The century is then used to find the anchor day for that specific century. This anchor day remains the same for all years within that century. The anchor days for different centuries are predefined and based on the doomsday algorithm rules.

Determining the year anchor day

Take the last two digits of the year. Divide these two digits by 12 to get the quotient and the remainder. Divide the remainder by 4 to get the number of leap years. Add the quotient, remainder, and the number of leap years together. Finally, take this sum modulo 7 (result mod 7) to obtain the year anchor day.

Determining the month anchor day

Retrieve the corresponding number for the given month. The month anchor day is simply the assigned number. Assign a specific number to each month as follows −

  • January − 3

  • February − 0

  • March − 0

  • April − 4

  • May − 9

  • June − 6

  • July − 11

  • August − 8

  • September − 5

  • October − 10

  • November − 7

  • December − 12

Calculating the dooms day

Add the century anchor day, year anchor day, and month anchor day together. Take this sum modulo 7 (result mod 7) to obtain the doomsday. The doomsday is represented by an integer from 0 to 6, where 0 corresponds to Sunday, 1 to Monday, and so on.

Example

We can calculate the dooms day by using python with the following code by implementing the above mentioned steps one by one.

def calculate_doomsday(year, month, day):
   # Calculate the century anchor day
   century = year // 100
   anchor = (5 * (century % 4) + 2) % 7

   # Calculate the year anchor day
   year_within_century = year % 100
   quotient = year_within_century // 12
   remainder = year_within_century % 12
   num_leap_years = year_within_century // 4
   year_anchor = (anchor + quotient + remainder + num_leap_years) % 7

   # Calculate the month anchor day
   month_anchors = [3, 0, 0, 4, 9, 6, 11, 8, 5, 10, 7, 12]
   month_anchor = month_anchors[month - 1]

   # Calculate the weekday
   day_of_week = (year_anchor + month_anchor + day) % 7
   return day_of_week

year = 1995
month = 1
day = 22
day_of_week = calculate_doomsday(year, month, day)
days_of_week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
print(f"The Dooms day of the week for {month}/{day}/{year} is {days_of_week[day_of_week]}.")

Output

The Dooms day of the week for 1/22/1995 is Saturday.

Updated on: 02-Aug-2023

68 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements