<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dreamshire &#187; Dates</title>
	<atom:link href="http://blog.dreamshire.com/tag/dates/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.dreamshire.com</link>
	<description>Showing what's behind the curtain</description>
	<lastBuildDate>Wed, 01 Sep 2010 02:23:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Project Euler Problem 19 Solution</title>
		<link>http://blog.dreamshire.com/2009/03/26/project-euler-problem-19-solution/</link>
		<comments>http://blog.dreamshire.com/2009/03/26/project-euler-problem-19-solution/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 21:40:28 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 10-19]]></category>
		<category><![CDATA[Dates]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=72</guid>
		<description><![CDATA[How many Sundays fell on the first of the month during the twentieth century?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>You are given the following information, but you may prefer to do some research for yourself.</p>
<p>1 Jan 1900 was a Monday.<br />
Thirty days has September,<br />
April, June and November.<br />
All the rest have thirty-one,<br />
Saving February alone,<br />
Which has twenty-eight, rain or shine.<br />
And on leap years, twenty-nine. </p>
<p>A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.</p>
<p>How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?</p>
<h4><u>Analysis</u></h4>
<p>There are 1200 months (therefore 1200 firsts of the month) in 100 years (1/1/1901 – 12/31/2000).  One in every 7 days is a Sunday (or Monday, Tuesday, etc.)  which are (roughly) uniformly distributed over the date range.  An estimate of 1200/7 Sundays should be close to the answer.  As the date range widens this estimate becomes less precise.</p>
<p>We wrote a quick piece of JavaScript (great native calendar support) to prove it to ourselves. </p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Javascript.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> count<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> y<span style="color: #339933;">=</span><span style="color: #CC0000;">1901</span><span style="color: #339933;">,</span> years<span style="color: #339933;">=</span><span style="color: #CC0000;">100</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> m<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> m<span style="color: #339933;">&lt;</span><span style="color: #CC0000;">12</span><span style="color: #339933;">*</span>years<span style="color: #339933;">;</span> m<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span>y<span style="color: #339933;">,</span>m<span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getDay</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> count <span style="color: #339933;">++;</span>
&nbsp;
document.<span style="color: #000066; font-weight: bold;">write</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'answer to PE19 = '</span> <span style="color: #339933;">+</span> count <span style="color: #339933;">+</span> <span style="color: #3366CC;">' Sundays'</span><span style="color: #009900;">&#41;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>JavaScript numbers the months starting from 0 to 11 so month m=12 is the 1st month (January) of the next year (1902) and so month m=24 is January 1903.  Also, getDay() returns 0 for Sunday.  As an exercise you could pose questions for other date ranges and other days of the week and compare it to the estimating formula.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/03/26/project-euler-problem-19-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
