Program to calculate Dooms Day for a year

The doomsday algorithm is a mathematical method developed by John Horton Conway to determine the day of the week for any given date. It's based on the concept that certain dates within each year always fall on the same day of the week, called the doomsday.

The doomsday occurs on the following memorable dates

  • January 3rd (January 4th in leap years)

  • February 28th (February 29th in leap years)

  • March 7th

  • April 4th

  • May 9th

  • June 6th

  • July 11th

  • August 8th

  • September 5th

  • October 10th

  • November 7th

  • December 12th

Algorithm Steps

Step 1: Calculate Century Anchor Day

The century anchor day serves as a reference point for the entire century. It's calculated using the formula: (5 * (century % 4) + 2) % 7.

Step 2: Calculate Year Anchor Day

Take the last two digits of the year and follow these steps

  • Divide by 12 to get quotient and remainder

  • Divide the remainder by 4 to get leap year adjustments

  • Add century anchor + quotient + remainder + leap adjustments

  • Take the result modulo 7

Step 3: Calculate Month Offset

Each month has a specific offset value

  • January 3, February 0, March 0

  • April 4, May 9, June 6

  • July 11, August 8, September 5

  • October 10, November 7, December 12

Python Implementation

def calculate_doomsday(year, month, day):
    # Step 1: Calculate century anchor day
    century = year // 100
    century_anchor = (5 * (century % 4) + 2) % 7
    
    # Step 2: Calculate year anchor day
    year_within_century = year % 100
    quotient = year_within_century // 12
    remainder = year_within_century % 12
    leap_adjustments = remainder // 4
    year_anchor = (century_anchor + quotient + remainder + leap_adjustments) % 7
    
    # Step 3: Month offsets
    month_offsets = [3, 0, 0, 4, 9, 6, 11, 8, 5, 10, 7, 12]
    month_offset = month_offsets[month - 1]
    
    # Step 4: Calculate final day of week
    day_of_week = (year_anchor + month_offset + day) % 7
    return day_of_week

# Test with example date
year = 1995
month = 1
day = 22

result = calculate_doomsday(year, month, day)
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

print(f"Date: {month}/{day}/{year}")
print(f"Day of week: {days[result]}")
Date: 1/22/1995
Day of week: Sunday

Multiple Examples

def calculate_doomsday(year, month, day):
    century = year // 100
    century_anchor = (5 * (century % 4) + 2) % 7
    
    year_within_century = year % 100
    quotient = year_within_century // 12
    remainder = year_within_century % 12
    leap_adjustments = remainder // 4
    year_anchor = (century_anchor + quotient + remainder + leap_adjustments) % 7
    
    month_offsets = [3, 0, 0, 4, 9, 6, 11, 8, 5, 10, 7, 12]
    month_offset = month_offsets[month - 1]
    
    day_of_week = (year_anchor + month_offset + day) % 7
    return day_of_week

# Test multiple dates
test_dates = [
    (2000, 1, 1),   # New Year 2000
    (2024, 7, 4),   # Independence Day 2024
    (1969, 7, 20),  # Moon landing
]

days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

for year, month, day in test_dates:
    result = calculate_doomsday(year, month, day)
    print(f"{month}/{day}/{year} was a {days[result]}")
1/1/2000 was a Saturday
7/4/2024 was a Thursday
7/20/1969 was a Sunday

Conclusion

The doomsday algorithm provides an efficient way to calculate the day of the week for any date. By understanding century anchors, year calculations, and month offsets, you can determine weekdays without relying on calendar libraries.

Updated on: 2026-03-27T10:46:58+05:30

425 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements