Skip to content

Instantly share code, notes, and snippets.

@jonsagara
Last active February 1, 2018 17:09
Show Gist options
  • Save jonsagara/940f2ac9cb61a55a0dcf906e5df6005c to your computer and use it in GitHub Desktop.
Save jonsagara/940f2ac9cb61a55a0dcf906e5df6005c to your computer and use it in GitHub Desktop.
How many Friday the 13ths occur in a year?
How many Friday the 13ths occur in a year?
2000: 1
2001: 2
2002: 2
2003: 1
2004: 2
2005: 1
2006: 2
2007: 2
2008: 1
2009: 3
2010: 1
2011: 1
2012: 3
2013: 2
2014: 1
2015: 3
2016: 1
2017: 2
2018: 2
void Main()
{
var startYear = 2000;
var endYear = DateTime.UtcNow.Year;
var spookyYears = new List<(int year, int occurrences)>();
for (var ixYear = startYear; ixYear <= endYear; ixYear++)
{
var spookyDaysThisYear = 0;
for (var ixMonth = 1; ixMonth <= 12; ixMonth++)
{
var spookyDay = new DateTime(ixYear, ixMonth, 13);
if (spookyDay.DayOfWeek == DayOfWeek.Friday)
{
spookyDaysThisYear++;
}
}
spookyYears.Add((year: ixYear, occurrences: spookyDaysThisYear));
}
Console.WriteLine("How many Friday the 13ths occur in a year?");
foreach (var (year, occurrences) in spookyYears)
{
Console.WriteLine($"{year}: {occurrences}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment