Open In App

Program to calculate Dooms Day for a year

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Doomsday may refer to a hypothetical event according to which the end of human life is at the highest possibility. There are many algorithms written to calculate which day of a week in a year has the highest possibility of doomsday falling on that day.
All the statements are with respect to Gregorian Calendar. As the Gregorian calendar repeats itself every 400 years so a set of rules is decided for 1st 400 years only. Algorithms are derived from the calculations of John Conway, Lewis Carroll and many other mathematicians in history who worked on the calculation of Dooms Day.

To calculate Doom’s Day of a particular year following algorithm is used:-

  • Extract the last two digits of the year.(Let this be y)
  • Divide by 12 take the floor of the value.
  • Then add remainder of dividing y by 12.
  • Calculate the result when remainder of y divided by 12 is divided by 4.
  • Take the floor of the above value and then add.
  • Take remainder after dividing with 7 (mod 7).
  • Add the value of anchor day of the week starting from Sunday (Considering Sunday as 0)

The formula becomes –

([y/12]+ y mod 12 + [y mod 12/4]) mod 7 + anchor
 Here [ ] is Greatest Integer Function.
Anchor day changes after 100 years and repeats after every 400 years in the following way –

0-99 yrs --> Tuesday
100-199 yrs --> Sunday
200-299 yrs --> Friday
300-399 yr --> Wednesday

After this the above anchor days repeat as mentioned in the beginning of the article.
Examples:

Input :  2005
Output : Doomsday in the year 2005 = Monday

Input : 1800
Output : Doomsday in the year 1800 = Friday

Below is the implementation.

C++14

#include<bits/stdc++.h>
using namespace std;
 
string dooms_day(int year)
{
     
    // map to store days value of
    // anchor day can be known
    map<int, string> dict_day;
    dict_day[0] = "Sunday";
    dict_day[1] = "Monday";
    dict_day[2] = "Tuesday";
    dict_day[3] = "Wednesday";
    dict_day[4] = "Thursday";
    dict_day[5] = "Friday";
    dict_day[6] = "Saturday";
     
    // Gregorian calendar repeats
    // every 400 years
    int k = year % 400;
     
    int anchor;
     
    // Decide the anchor day
    if(k >= 0 && k < 100)
        anchor = 2;
         
    else if(k >= 100 && k < 200)
        anchor = 0;
         
    else if(k >= 200 && k < 300)
        anchor = 5;
         
    else
        anchor = 3;
         
    int y = year % 100;
     
    // Dooms day formula by Conway
    int doomsday = ((y / 12 + y % 12 +
                    (y % 12) / 4) % 7 + anchor) % 7;
     
    return dict_day[doomsday];
}
 
// Driver code
int main()
{
    int year = 1966;
     
    cout << "Doomsday in the year "
         << year << " = " << dooms_day(year);
     
    return 0;
}
 
// This code is contributed by yatinagg

                    

Java

import java.util.*;
 
class GFG{
     
public static String dooms_day(int year)
{
     
    // map to store days value of 
    // anchor day can be known 
    HashMap<Integer, String> dict_day = new HashMap<>();
    dict_day.put(0, "Sunday");
    dict_day.put(1, "Monday");
    dict_day.put(2, "Tuesday");
    dict_day.put(3, "Wednesday");
    dict_day.put(4, "Thursday");
    dict_day.put(5, "Friday");
    dict_day.put(6, "Saturday");
       
    // Gregorian calendar repeats 
    // every 400 years 
    int k = year % 400;
       
    int anchor;
       
    // Decide the anchor day 
    if (k >= 0 && k < 100)
        anchor = 2;
           
    else if (k >= 100 && k < 200)
        anchor = 0;
           
    else if (k >= 200 && k < 300)
        anchor = 5;
           
    else
        anchor = 3;
           
    int y = year % 100;
       
    // Dooms day formula by Conway 
    int doomsday = ((y / 12 + y % 12
                   (y % 12) / 4) % 7 +
                    anchor) % 7;
       
    return dict_day.get(doomsday);
}
 
// Driver code
public static void main(String[] args)
{
    int year = 1966;
     
    System.out.println("Doomsday in the year " +
                        year + " = " +
                        dooms_day(year));
}
}
 
// This code is contributed divyeshrabadiya07

                    

Python3

def dooms_day(year):
     
    # dictionary to store days
    # value of anchor day can be known
    dict_day ={ 0 : "Sunday",
               1 : "Monday",
               2 : "Tuesday",
               3 : "Wednesday",
               4 : "Thursday",
               5 : "Friday",
               6 : "Saturday" }
     
    # gregorian calendar repeats
    # every 400 years
    k = year % 400
     
    # decide the anchor day
    if(k >= 0 and k < 100):
        anchor = 2
         
    elif(k >= 100 and k < 200):
        anchor = 0
         
    elif(k >= 200 and k < 300):
        anchor = 5
         
    else:
        anchor = 3
         
    y = year % 100
     
    # dooms day formula by Conway
    doomsday = ((y//12 + y % 12 + (y % 12)//4)% 7 + anchor) % 7
     
    return dict_day[doomsday]
 
# Driver code
year = 1966
print("Doomsday in the year % s = % s"%(year,
                                        dooms_day(year)))

                    

C#

using System;
using System.Collections.Generic;  
class GFG
{  
    static String dooms_day(int year)
    {
          
        // map to store days value of 
        // anchor day can be known 
        Dictionary<int, string> dict_day =  
                       new Dictionary<int, string>(); 
        dict_day.Add(0, "Sunday");
        dict_day.Add(1, "Monday");
        dict_day.Add(2, "Tuesday");
        dict_day.Add(3, "Wednesday");
        dict_day.Add(4, "Thursday");
        dict_day.Add(5, "Friday");
        dict_day.Add(6, "Saturday");
            
        // Gregorian calendar repeats 
        // every 400 years 
        int k = year % 400;
            
        int anchor;
            
        // Decide the anchor day 
        if (k >= 0 && k < 100)
            anchor = 2;
                
        else if (k >= 100 && k < 200)
            anchor = 0;
                
        else if (k >= 200 && k < 300)
            anchor = 5;
                
        else
            anchor = 3;
                
        int y = year % 100;
            
        // Dooms day formula by Conway 
        int doomsday = ((y / 12 + y % 12 + 
                       (y % 12) / 4) % 7 +
                        anchor) % 7;
            
        return dict_day[doomsday];
    }
   
  // Driver code
  static void Main()
  {
        int year = 1966;
      
        Console.WriteLine("Doomsday in the year " +
                            year + " = " +
                            dooms_day(year));
  }
}
 
// This code is contributed by divyesh072019

                    

Javascript

<script>
      function dooms_day(year) {
 
        // map to store days value of
        // anchor day can be known
        var dict_day = new Map();
        dict_day.set(0, "Sunday");
        dict_day.set(1, "Monday");
        dict_day.set(2, "Tuesday");
        dict_day.set(3, "Wednesday");
        dict_day.set(4, "Thursday");
        dict_day.set(5, "Friday");
        dict_day.set(6, "Saturday");
 
        // Gregorian calendar repeats
        // every 400 years
        var k = year % 400;
 
        var anchor;
 
        // Decide the anchor day
        if (k >= 0 && k < 100)
            anchor = 2;
 
        else if (k >= 100 && k < 200)
            anchor = 0;
 
        else if (k >= 200 && k < 300)
            anchor = 5;
 
        else
            anchor = 3;
 
        var y = parseInt(year % 100);
 
        // Dooms day formula by Conway
        var doomsday = parseInt(parseInt(parseInt(y / 12) + y % 12 + parseInt((y % 12) / 4)) % 7 + anchor) % 7;
 
        return dict_day.get(doomsday);
    }
 
    // Driver code
        var year = 1966;
        document.write("Doomsday in the year " + year + " = " + dooms_day(year));
 
// This code is contributed by gauravrajput1
</script>

                    

Output: 
Doomsday in the year 1966 = Monday

 

Time Complexity: O (1)
Space Complexity: O (1)

Method 2:

  • Divide the year by 12 and take the quotient (integer division) and remainder. The quotient represents the number of 12-year cycles that have passed since the year 0, and the remainder represents the number of years that have passed since the last complete 12-year cycle.
  • Add the remainder to itself divided by 4. This gives the number of leap years that have occurred since the last complete 12-year cycle.
  • Add the anchor day for the 12-year cycle (which can be memorized using a mnemonic) to the result from step 2.
  • Take the result from step 3 modulo 7. This gives the doomsday for the year.

Here’s an implementation of this method:

Python3

def dooms_day(year):
    # dictionary to store days
    # value of anchor day can be known
    dict_day ={ 0 : "Sunday",
               1 : "Monday",
               2 : "Tuesday",
               3 : "Wednesday",
               4 : "Thursday",
               5 : "Friday",
               6 : "Saturday" }
 
    # calculate the doomsday using the alternative method
    anchor_days = [3, 28, 14, 4, 25, 9, 30, 18, 6, 27, 11, 1]
    q, r = divmod(year, 12)
    num_leap_years = r // 4
    doomsday = (num_leap_years + anchor_days[r % 12] + q) % 7
 
    return dict_day[doomsday]
print("DoomsDay in the year 1966 : ", end = "")
print(dooms_day(1966))

                    

Javascript

function dooms_day(year) {
    // dictionary to store days
    // value of anchor day can be known
    const dict_day = { 0 : "Sunday",
                       1 : "Monday",
                       2 : "Tuesday",
                       3 : "Wednesday",
                       4 : "Thursday",
                       5 : "Friday",
                       6 : "Saturday" };
     
    // calculate the doomsday using the alternative method
    const anchor_days = [3, 28, 14, 4, 25, 9, 30, 18, 6, 27, 11, 1];
    const [q, r] = [Math.floor(year / 12), year % 12];
    const num_leap_years = Math.floor(r / 4);
    const doomsday = (num_leap_years + anchor_days[r % 12] + q) % 7;
     
    return dict_day[doomsday];
}
 
console.log("Doomsday in the year 1966 : " + dooms_day(1966))

                    

Java

import java.util.HashMap;
import java.util.Map;
 
public class GFG {
    public static String doomsDay(int year)
    {
        // dictionary to store days
        // value of anchor day can be known
        Map<Integer, String> dict_day
            = new HashMap<Integer, String>() {
                  {
                      put(0, "Sunday");
                      put(1, "Monday");
                      put(2, "Tuesday");
                      put(3, "Wednesday");
                      put(4, "Thursday");
                      put(5, "Friday");
                      put(6, "Saturday");
                  }
              };
 
        // calculate the doomsday using the alternative
        // method
        int[] anchor_days
            = { 3, 28, 14, 4, 25, 9, 30, 18, 6, 27, 11, 1 };
        int[] divmod = { year / 12, year % 12 };
        int num_leap_years = divmod[1] / 4;
        int doomsday
            = (num_leap_years + anchor_days[divmod[1] % 12]
               + divmod[0])
              % 7;
 
        return dict_day.get(doomsday);
    }
 
    public static void main(String[] args)
    {
        System.out.print("DoomsDay in the year 1966 : ");
        System.out.println(doomsDay(1966));
    }
}

                    

C++

//GeeksforGeeks
//C++ code for this approach
#include <iostream>
#include <unordered_map>
using namespace std;
 
string doomsDay(int year) {
// dictionary to store days
// value of anchor day can be known
unordered_map<int, string> dict_day
= { {0, "Sunday"}, {1, "Monday"}, {2, "Tuesday"},
{3, "Wednesday"}, {4, "Thursday"}, {5, "Friday"},
{6, "Saturday"} };
  // calculate the doomsday using the alternative
// method
int anchor_days[]
    = { 3, 28, 14, 4, 25, 9, 30, 18, 6, 27, 11, 1 };
int divmod[] = { year / 12, year % 12 };
int num_leap_years = divmod[1] / 4;
int doomsday
    = (num_leap_years + anchor_days[divmod[1] % 12]
       + divmod[0])
      % 7;
 
return dict_day[doomsday];
}
 
int main() {
cout << "DoomsDay in the year 1966 : " << doomsDay(1966) << endl;
return 0;
}
 
// This code is written by Sundaram

                    

C#

using System;
using System.Collections.Generic;
 
public class GFG {
    public static string DoomsDay(int year)
    {
        // dictionary to store days
        // value of anchor day can be known
        Dictionary<int, string> dictDay = new Dictionary<int, string>()
        {
            { 0, "Sunday" },
            { 1, "Monday" },
            { 2, "Tuesday" },
            { 3, "Wednesday" },
            { 4, "Thursday" },
            { 5, "Friday" },
            { 6, "Saturday" }
        };
 
        // calculate the doomsday using the alternative
        // method
        int[] anchorDays = { 3, 28, 14, 4, 25, 9, 30, 18, 6, 27, 11, 1 };
        int[] divmod = { year / 12, year % 12 };
        int numLeapYears = divmod[1] / 4;
        int doomsday = (numLeapYears + anchorDays[divmod[1] % 12] + divmod[0]) % 7;
 
        return dictDay[doomsday];
    }
 
    public static void Main(string[] args)
    {
        Console.Write("DoomsDay in the year 1966 : ");
        Console.WriteLine(DoomsDay(1966));
    }
}

                    
Output: 

Doomsday in the year 1966 = Monday

Time complexity: O(1) because it performs a fixed number of arithmetic operations and array accesses, regardless of the input year.
Auxiliary space: O(1) because it only uses a fixed amount of memory to store the dict_day dictionary and the anchor_days list, which are both constant in size. 



Last Updated : 27 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads