Problem: You are given the following information, but you may prefer to do some research for yourself.

• 1 Jan 1900 was a Monday.
• Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
• A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

### Approach

There are several different ways to approach this. The easiest, I think, is to use the Gaussian formula for day of week. It is a purely mathematical formula that I have encoded in the following Python code.

### Python Solution

import time from math import floor   """ Gaussian algorithm to determine day of week """ def day_of_week(year, month, day): """ w = (d+floor(2.6*m-0.2)+y+floor(y/4)+floor(c/4)-2*c) mod 7   Y = year - 1 for January or February Y = year for other months d = day (1 to 31) m = shifted month (March = 1, February = 12) y = last two digits of Y c = first two digits of Y w = day of week (Sunday = 0, Saturday = 6) """   d = day m = (month - 3) % 12 + 1 if m &gt; 10: Y = year - 1 else: Y = year y = Y % 100 c = (Y - (Y % 100)) / 100   w = (d + floor(2.6 * m - 0.2) + y + floor(y/4) + floor(c/4) - 2*c) % 7   return int(w)   """ Compute the number of months starting on a given day of the week in a century """ def months_start_range(day,year_start,year_end): total = 0 for year in range(year_start, year_end + 1): for month in range(1,13): if day_of_week(year, month, 1) == day: total += 1 return total   start = time.time()   total = months_start_range(0,1901,2000)   elapsed = time.time() - start   print("%s found in %s seconds") % (total,elapsed)

This returns the correct result.

171 found in 0.0681998729706 seconds

That will run faster if executed directed in Python. Remember, I’m using the Sage notebook environment to execute most Python here. I’m also writing Cython in the same environment.

### Cython Solution

There is a nearly trivial rewriting to Cython of the above Python code.

%cython   import time from math import floor   """ Gaussian algorithm to determine day of week """ cdef day_of_week(int year, int month, int day): """ w = (d+floor(2.6*m-0.2)+y+floor(y/4)+floor(c/4)-2*c) mod 7   Y = year - 1 for January or February Y = year for other months d = day (1 to 31) m = shifted month (March = 1, February = 12) y = last two digits of Y c = first two digits of Y w = day of week (Sunday = 0, Saturday = 6) """   cdef int d = day cdef int m = (month - 3) % 12 + 1 cdef int Y if m &gt; 10: Y = year - 1 else: Y = year y = Y % 100 cdef int c = (Y - (Y % 100)) / 100   cdef double w w = (d + floor(2.6 * m - 0.2) + y + floor(y/4) + floor(c/4) - 2*c) % 7   return int(w)   """ Compute the number of months starting on a given day of the week in range of years """ cdef months_start_range(int day, int year_start,int year_end): cdef unsigned int total = 0 cdef int year, month for year in range(year_start, year_end + 1): for month in range(1,13): if day_of_week(year, month, 1) == day: total += 1 return total   start = time.time()   total = months_start_range(0,1901,2000)   elapsed = time.time() - start   print("%s found in %s seconds") % (total,elapsed)

The code is a bit longer, but it executes much faster.

171 found in 0.00387215614319 seconds

The Cython code runs roughly 18 times faster.

### C Solution

The Cython code was used as a model to create more efficient C code. The only issue here is in maintaining the correct datatypes (not too hard, but compared to Cython it is a pain).

#include <stdio.h> #include <stdlib.h> #include <math.h>   int day_of_week(int year, int month, int day) { // Using the Gaussian algorithm int d = day;   double m = (double) ((month - 3) % 12 + 1); int Y; if(m > 10) Y = year - 1; else Y = year; int y = Y % 100; int c = (Y - (Y % 100)) / 100;   int w = ((d+(int)floor(2.6*m-0.2)+y+ y/4 + c/4 -2*c))%7;   return w; }   long months_start_range(int day, int year_start, int year_end) { unsigned long total = 0; int year, month; for(year = year_start; year < year_end; year++) { for(month = 1; month <= 12; month++) { if(day_of_week(year, month, 1)==day) total++; } } return total;   }   int main(int argc, char **argv) {   int iter = 0; long total; while(iter < 100000) { total = months_start_range(0,1901,2000); iter++; } printf("Solution: %ld\n",total);   return 0; }

Notice that this executes the loop 100,000 times, as I’m trying to get a good idea of what the average runtime is. We compile with optimization and the [[[-lm]]] math option. We get the following result.

gcc -O3 -o problem-19 problem-19.c -lm \$ time ./problem-19 Solution: 171   real 0m6.240s

The C code runs roughly 62 times as fast as the Cython and roughly 1124 times as fast as the Python. Each iteration executes in about 6.2000e-5 seconds.