Program to calculate Dooms Day for a year
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 –
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< 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" ;
int k = year % 400;
int anchor;
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;
int doomsday = ((y / 12 + y % 12 +
(y % 12) / 4) % 7 + anchor) % 7;
return dict_day[doomsday];
}
int main()
{
int year = 1966;
cout << "Doomsday in the year "
<< year << " = " << dooms_day(year);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static String dooms_day( int year)
{
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" );
int k = year % 400 ;
int anchor;
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 ;
int doomsday = ((y / 12 + y % 12 +
(y % 12 ) / 4 ) % 7 +
anchor) % 7 ;
return dict_day.get(doomsday);
}
public static void main(String[] args)
{
int year = 1966 ;
System.out.println( "Doomsday in the year " +
year + " = " +
dooms_day(year));
}
}
|
Python3
def dooms_day(year):
dict_day = { 0 : "Sunday" ,
1 : "Monday" ,
2 : "Tuesday" ,
3 : "Wednesday" ,
4 : "Thursday" ,
5 : "Friday" ,
6 : "Saturday" }
k = year % 400
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
doomsday = ((y / / 12 + y % 12 + (y % 12 ) / / 4 ) % 7 + anchor) % 7
return dict_day[doomsday]
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)
{
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" );
int k = year % 400;
int anchor;
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;
int doomsday = ((y / 12 + y % 12 +
(y % 12) / 4) % 7 +
anchor) % 7;
return dict_day[doomsday];
}
static void Main()
{
int year = 1966;
Console.WriteLine( "Doomsday in the year " +
year + " = " +
dooms_day(year));
}
}
|
Javascript
<script>
function dooms_day(year) {
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" );
var k = year % 400;
var anchor;
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);
var doomsday = parseInt(parseInt(parseInt(y / 12) + y % 12 + parseInt((y % 12) / 4)) % 7 + anchor) % 7;
return dict_day.get(doomsday);
}
var year = 1966;
document.write( "Doomsday in the year " + year + " = " + dooms_day(year));
</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):
dict_day = { 0 : "Sunday" ,
1 : "Monday" ,
2 : "Tuesday" ,
3 : "Wednesday" ,
4 : "Thursday" ,
5 : "Friday" ,
6 : "Saturday" }
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) {
const dict_day = { 0 : "Sunday" ,
1 : "Monday" ,
2 : "Tuesday" ,
3 : "Wednesday" ,
4 : "Thursday" ,
5 : "Friday" ,
6 : "Saturday" };
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)
{
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" );
}
};
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++
#include <iostream>
#include <unordered_map>
using namespace std;
string doomsDay( int year) {
unordered_map< int , string> dict_day
= { {0, "Sunday" }, {1, "Monday" }, {2, "Tuesday" },
{3, "Wednesday" }, {4, "Thursday" }, {5, "Friday" },
{6, "Saturday" } };
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;
}
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static string DoomsDay( int year)
{
Dictionary< int , string > dictDay = new Dictionary< int , string >()
{
{ 0, "Sunday" },
{ 1, "Monday" },
{ 2, "Tuesday" },
{ 3, "Wednesday" },
{ 4, "Thursday" },
{ 5, "Friday" },
{ 6, "Saturday" }
};
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
Share your thoughts in the comments
Please Login to comment...