[C++] Amazing Algorithm

binh12A3
Nov 25, 2023

WTF algorithms …

1. Sakamoto algorithm

Check what day of the week it is in just a few lines of code

#include <iostream>

using namespace std;

// SAKAMOTO ALGORITHM
int dow(int y, int m, int d)
{
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7 + 1;
}

int main()
{
int day_of_week = dow(2023, 11, 26);
cout << "Today is " << day_of_week << endl;

return 0;
}

--

--