Tomohiko Sakamoto’s Algorithm- Finding the day of the week


In this article, we will discuss what is Tomohiko Sakamoto’s algorithm and how this algorithm is used to identify which day of the week does the given date occurs. There are multiple algorithms to know the day of the week but this algorithm is the most powerful one. This algorithm finds the day of the month on which the date occurs in least possible time and least space complexity.

Problem statement − We are given a date as per Georgian calendar and our task is to find out which day of the week occurs on the given date using Tomohiko Sakamoto’s algorithm.

Example

Input − Date = 30, Month = 04, Year = 2020

Output − The given date is on Monday

Input − Date = 15, Month = 03, Year = 2012

Output − The given date is on Thursday

Input − Date = 24, Month = 12, Year = 2456

Output − The given date is on Sunday

Tomohiko Sakamoto’s algorithm

Let us now discuss the intuition behind the Tomohiko Sakamoto’s algorithm.

As we know, according to Georgian calendar, 1st of January 1 AD was a Monday.

Case 1 Ignoring Leap Year

Let us first discuss the case ignoring all the leap years, which means a year has 365 days in total.

As January has 31 days and there are 7 days in a week, we can say there are 7*4 + 3 days in January which means, first day of February is always 3 days after the first day of January.

As February has 28 days (except the cases of leap year), which itself is a multiple of 7 we can say that 1st of march will be on the same day as 1st of February which means 1st of march will also be 3 days after the day of 1st of January.

Now for April, we have 31 days in march which is 7*4 +3 which means it will occur after 3 days of the day on 1st of march. Hence, we can say 1st of April will occur on the day 6 days after 1st day of 1st January.

We will now construct an array in which arr[i] signifies the extra number of days after which ith month will occur in reference with the day on January 1st.

We have arr[] = { 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }.

Case 2 Leap Years

Let us now discuss the situation of leap years.

Every four years, a day is added in our calculation, with the exception of every hundred years when it is not. We must account for these extra days. To do so, we will use the formula-

year/ 4 (for every 4 years)

– year / 100 (for every 100th year which is a multiple of 4 but still not a leap year, we will remove it from leap years)

+ year/ 400(for every 400th year which is a multiple of 100 but still is a loop year )

This formula will provide us with the exact number of leap years. However, there is one exception to this.

Now, as we know February 29th is considered as a leap day and not January 0.

This implies that we do not need to include the first two moths of the year into our calculation as the leap day has no effect on it. So in case of January or February, we will subtract 1 from the year to compensate. Therefore, during these months, the value of year/4 should be based on previous year and not on the current one.

To address the leap year issue, we can make changes in the arr[] values of every month after February by subtracting 1, which would fill in the gap. This will solve the problem of the leap year. We need to implement the following changes in the algorithm to make it functional for both leap and non-leap years.

arr[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 }

If the current month is January or February, we need to decrement year by 1.

We need to modify the annual increment inside the modulus to year + year/4 – year/100 + year/400 instead of year. This change is necessary to account for the extra day in a leap year and adjust the calculation accordingly.

Example

The code for this approach is:

#include <bits/stdc++.h>
using namespace std;
// code to find out the day number of the given date
int Weekday(int year, int month, int day) {
   int arr[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
	
   if ( month < 3 )
      year -= 1;
   return ( ( year + year / 4 - year / 100 + year / 400 + arr[month - 1] + day) % 7 );
}

int main(void) {
   int day = 9, month = 9, year = 2020;
   int d = Weekday(year, month, day);
   string days[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
   cout<< " The given date is on "; 
   cout << days[d];
   return 0;
}

Output

The given date is on Wednesday

Complexities

Time complexity − The time complexity for this approach is O(1)

Space Complexity − Space complexity for this approach is O(1) as we are not using any additional space.

Conclusion − In this article, we have discussed about Tomohiko Sakamoto’s Algorithm and the intuition behind the algorithm

Updated on: 16-Aug-2023

138 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements