From 009ce017d99a8deb2089438381c273e8f3ea3b67 Mon Sep 17 00:00:00 2001 From: mjfernez Date: Fri, 6 Mar 2020 18:55:37 -0500 Subject: Added #19 Counting Sundays --- 19-Counting-Sundays/countsundays.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 19-Counting-Sundays/countsundays.c (limited to '19-Counting-Sundays/countsundays.c') diff --git a/19-Counting-Sundays/countsundays.c b/19-Counting-Sundays/countsundays.c new file mode 100644 index 0000000..e3f4d5a --- /dev/null +++ b/19-Counting-Sundays/countsundays.c @@ -0,0 +1,29 @@ +#include +#include + +// Implemented a timing routine for fun :) + +int main(){ + clock_t now, then; + int c = 0; + struct tm time = {0}; + + then = clock(); + time.tm_mday = 1; + + // Starting from 1-1-1901 + for(int y = 1; y <= 100; y++){ + for(int m = 0; m < 12; m++){ // Apparently January is the 0th month, + time.tm_mon = m; // even though there's no 0th day.... wtf K&R?? + time.tm_year = y; + time_t inttime = mktime(&time); + struct tm *t = localtime(&inttime); + if(t->tm_wday == 0) + c++; + } + } + printf("%d\n", c); + now = clock(); + printf("Only took %f ms\n", (double)(now - then) / (CLOCKS_PER_SEC / 1000)); + return 0; +} -- cgit v1.2.3