Five Weekends
October 16, 2018
This task comes to us from Rosetta Code: The month of October 2010 has five Fridays, five Saturdays and five Sundays.
Your task is to count and make a list of all months from 1900 to 2100, inclusive, that have five Fridays, Saturdays and Sundays, and to count and make a list of all years from 1900 to 2100, inclusive, that have no such months. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
I saw this as one of those viral posts on Facebook a few years ago, claiming that this happened only once in a few hundred years, and was ‘lucky’ according to ancient Chinese custom.
At that point I debunked it using Excel, but here’s a quick Python version. It operates on the principle that only 31 day months which start on Friday have this characteristic.
Results:
/bin/sh is the language of choice. Find months for which /usr/bin/cal prints 31 on a line by itself.
Here’s a solution in C.
Output: