euler/ipython/EulerProblem019.ipynb

159 lines
4.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Euler Problem 19\n",
"\n",
"You are given the following information, but you may prefer to do some research for yourself.\n",
"\n",
"* 1 Jan 1900 was a Monday.\n",
"* Thirty days has September,\n",
" April, June and November.\n",
" All the rest have thirty-one,\n",
" Saving February alone,\n",
" Which has twenty-eight, rain or shine.\n",
" And on leap years, twenty-nine.\n",
"* A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.\n",
"\n",
"How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I remember someone doing this by estimation. So let's try that first and then code a solution:\n",
"\n",
"We have one hundred years which equal 1200 months. Every month starts with a weekday and $\\frac{1}{7}$ of all weekdays are Sundays. So, 1200 divided by 7 is 171 point something.\n",
"\n",
"Which turns out to be the correct solution.\n",
"\n",
"Let's still code a solution because we were not that smart back then. We create two generators, zip, them and search for 1st and Sunday."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"172\n"
]
}
],
"source": [
"def weekday_generator_function():\n",
" while True:\n",
" yield 'Monday'\n",
" yield 'Tuesday'\n",
" yield 'Wednesday'\n",
" yield 'Thursday'\n",
" yield 'Friday'\n",
" yield 'Saturday'\n",
" yield 'Sunday'\n",
" \n",
"def day_of_month_generator_function():\n",
" day, month, year = 1, 1, 1901\n",
" while year < 2001:\n",
" yield day\n",
" day += 1\n",
" if month == 2:\n",
" if year % 4 == 0 and (not year % 100 == 0 or year % 400 == 0):\n",
" if day == 30:\n",
" month += 1\n",
" day = 1\n",
" elif day == 29:\n",
" month += 1\n",
" day = 1\n",
" elif month in [9, 4, 6, 11] and day == 31:\n",
" day = 1\n",
" month += 1\n",
" elif month in [1, 3, 5, 7, 8, 10] and day == 32:\n",
" day = 1\n",
" month += 1\n",
" elif month == 12 and day == 32:\n",
" day = 1\n",
" month = 1\n",
" year += 1\n",
"\n",
" \n",
"ds = zip(weekday_generator_function(), day_of_month_generator_function())\n",
"s = len([1 for weekday, date in ds if weekday == \"Sunday\" and date == 1])\n",
"print(s)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Okay, this is actually kind of embarrassing. Why am I even zipping when I have the month right there in my function. Problem is, that my weekday generator starts with Monday, but Monday was the start in 1900 not in 1901. Let's try to do better. 1.1.1901 was a Tuesday, so we drop Monday from the generator."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"171\n"
]
}
],
"source": [
"wds = weekday_generator_function()\n",
"next(wds) # get rid of first Monday\n",
"ds = zip(wds, day_of_month_generator_function())\n",
"s = len([1 for weekday, date in ds if weekday == \"Sunday\" and date == 1])\n",
"print(s)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And finally, here we go. Actually not a dummy exercise and shows how imporessive the shortcut from the beginning is. Raw power is not always the best solution if we can also use brain."
]
}
],
"metadata": {
"completion_date": "Fri, 5 Sep 2014, 09:56",
"kernelspec": {
"display_name": "Python 3",
"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.5.4"
},
"tags": [
"weekdays",
"brain",
"brute force",
"leap year"
]
},
"nbformat": 4,
"nbformat_minor": 0
}