Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "ce0feff9-8e09-4427-b78c-c608946a9a9b", "metadata": {}, "source": [ "### Q19\n", "How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?" ] }, { "cell_type": "code", "execution_count": 1, "id": "9b2e4340-8c44-45ce-adeb-44ce64455df4", "metadata": {}, "outputs": [], "source": [ "month_days = {\n", " \"normal\":{\n", " 1:31, 2:28, 3:31, 4:30,\n", " 5:31, 6:30, 7:31, 8:31,\n", " 9:30, 10:31, 11:30, 12:31\n", " },\n", " \"leap\":{\n", " 1:31, 2:29, 3:31, 4:30,\n", " 5:31, 6:30, 7:31, 8:31,\n", " 9:30, 10:31, 11:30, 12:31\n", " }\n", "}\n", "\n", "def first_day_of_month(last_month_first_day, last_month, last_month_year):\n", " year_type = \"normal\"\n", " if (last_month_year%4 == 0 and last_month_year%100 != 0) or (last_month_year%400 == 0):\n", " year_type = \"leap\"\n", " first_day = (last_month_first_day+month_days[year_type][last_month])%7\n", " month = last_month+1 if last_month < 12 else 1\n", " year = last_month_year if last_month < 12 else last_month_year+1\n", " return first_day,month,year\n", "\n", "def first_day_of_month_in_range(start_info, end_info):\n", " # start_info = [day_of_first_day, month, year]\n", " # end_info = [month, year]\n", " first_day_of_month_list = [start_info[0]]\n", " iterating_info = start_info\n", " while True:\n", " iterating_info = first_day_of_month(iterating_info[0],iterating_info[1],iterating_info[2])\n", " if ((iterating_info[2] > end_info[1]) or \n", " (iterating_info[2] == end_info[1] and iterating_info[1] > end_info[0])):\n", " break\n", " first_day_of_month_list.append(iterating_info[0])\n", " return first_day_of_month_list" ] }, { "cell_type": "code", "execution_count": 2, "id": "58a4d119-c3c1-41e7-b577-9b2a60dc654d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 243 µs, sys: 63 µs, total: 306 µs\n", "Wall time: 307 µs\n" ] }, { "data": { "text/plain": [ "171" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "(first_day_of_month_in_range([1,1,1900],[12,2000]).count(0) \n", " - first_day_of_month_in_range([1,1,1900],[12,1900]).count(0))" ] }, { "cell_type": "code", "execution_count": null, "id": "3de13c29-0552-4834-ab26-540ef4a6d16e", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }
Previous Notebook:
Project Euler Q18
Next Notebook:
Project Euler Q20
Loading