Hi all!
This is just a random post to show off something new I found. I don’t claim credit for this algorithm and if you’re curious as to how it works then this Wikipedia article may be of interest to you. This post has a good explanation of why the algorithm works in the first place.
Given a date the following function would give a string donating the weekday on which a given date will fall:
public string FastDayOfTheWeek(int year, int month, int day) { string[] days = new string[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; if (month < 3) { --year; } int weekday = (year + (year / 4) - (year / 100) + (year / 400) + (int)("032504514624"[month - 1] - 48) + day) % 7; return days[weekday]; }
If you run the function for the 1st of March 2020 it will give a Sunday. This is the correct answer. This function works backwards and forwards in time as far as I can tell.
Another interesting thing about the given formula is that it could easily be memorised and may make for a nice mathematical party trick.
Hopefully someone out there will find this useful and or interesting.