Debunking the "five weekends every 823 years" myth with Perl

| 1 Comment

Have you seen this bit of bunk posted recently? It goes like this:

This year, July has five Fridays, five Saturdays and five Sundays. This happens once every 823 years. So: copy to your status and money will arrive within 4 days... based on Chinese Feng Shui.

Of course it's crap, and of course Snopes.com does a fine job of debunking it. But what if you want more evidence? Perl to the rescue! Here's a little program that finds and counts all the months with five full Fri-Sun weekends in the next 823 years.

#!/usr/bin/perl

# Debunking the five weekends myth
# http://www.snopes.com/inboxer/trivia/fivedays.asp

use strict;
use warnings;

use feature 'say';

use DateTime;

my $start = 2013;
my $end   = $start + 823 - 1;
say "Months with five full weekends between $start and $end";

my $nmonths = 0;
for my $year ( $start .. $end ) {
    for my $month ( 1..12 ) {
        my $eom = DateTime->last_day_of_month(
            year => $year, month => $month );
        if ( $eom->day == 31 && $eom->day_of_week == 7 ) {
            say $eom->month_name, ' ', $year;
            ++$nmonths;
        }
    }
}
say "There will be $nmonths months with five full 
    weekends in the 823 years between $start and $end.";


$ ./five-weekends
Months with five full weekends between 2013 and 2835
March 2013
August 2014
May 2015
...
October 2832
July 2833
December 2834
There will be 823 months with five full weekends in the 
    823 years between 2013 and 2835.

1 Comment

I created a C# version using (or rather: abusing) LINQ:
__________________

// Debunking the five weekends myth
// http://www.snopes.com/inboxer/trivia/fivedays.asp

using System;
using System.Globalization;
using System.Linq;

class Program
{
static void Main(string[] args)
{
var totalyears = 823;
var startDate = new DateTime(2013, 1, 1);
var endDate = startDate.AddYears(totalyears).AddDays(-1);

var months = Enumerable.Range(0, (int)(endDate - startDate).TotalDays)
.Select(d => startDate.AddDays(d))
.GroupBy(d => new { Y = d.Year, M = d.Month }, d => d)
.Where(d => (d.Where(gd => gd.DayOfWeek == DayOfWeek.Friday).Count() == 5)
&& (d.Where(gd => gd.DayOfWeek == DayOfWeek.Saturday).Count() == 5)
&& (d.Where(gd => gd.DayOfWeek == DayOfWeek.Sunday).Count() == 5)
).ToArray();

Console.WriteLine(string.Join("\r\n", months.Select(q => string.Format("{0} {1}",
q.Key.Y, CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(q.Key.M)))
));
Console.WriteLine(
@"There will be {0} months with five full weekends in the {1} years between {2} and {3}.",
months.Count(), totalyears, startDate.Year, endDate.Year
);
Console.ReadKey();
}
}


__________________
There's room for improvement though, just couldn't be bothered anymore :P

Leave a comment