aboutsummaryrefslogtreecommitdiffstats
path: root/19-Counting-Sundays/countsundays.c
diff options
context:
space:
mode:
Diffstat (limited to '19-Counting-Sundays/countsundays.c')
-rw-r--r--19-Counting-Sundays/countsundays.c29
1 files changed, 29 insertions, 0 deletions
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 <stdio.h>
+#include <time.h>
+
+// 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;
+}