<?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>James McMinn &#187; Project Euler</title>
	<atom:link href="http://jamesmcminn.com/category/project-euler/feed/" rel="self" type="application/rss+xml" />
	<link>http://jamesmcminn.com</link>
	<description>My little piece of the web</description>
	<lastBuildDate>Tue, 18 May 2010 19:13:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Project Euler</title>
		<link>http://jamesmcminn.com/2009/08/19/project-euler/</link>
		<comments>http://jamesmcminn.com/2009/08/19/project-euler/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 11:45:21 +0000</pubDate>
		<dc:creator>James McMinn</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Bruteforce]]></category>
		<category><![CDATA[Maths]]></category>

		<guid isPermaLink="false">http://james.ohthebanter.com/?p=57</guid>
		<description><![CDATA[I discovered Project Euler a few weeks ago while searching for something to keep my mind (and programming skills) busy during the holidays. Its proven itself to be exactly what I was looking for — if a bit more challenging than I was expecting.
Project Euler isn&#8217;t for everyone. If you don&#8217;t have a fairly decent [...]]]></description>
			<content:encoded><![CDATA[<p>I discovered <a title="Project Euler" href="http://projecteuler.net" target="_blank">Project Euler</a> a few weeks ago while searching for something to keep my mind (and programming skills) busy during the holidays. Its proven itself to be exactly what I was looking for — if a bit more challenging than I was expecting.</p>
<p>Project Euler isn&#8217;t for everyone. If you don&#8217;t have a fairly decent grasp of maths and the ability to do a bit of coding then you will struggle with the vast majority of the 250+ problems. <strong>These are not easy.</strong> Of course if you&#8217;re willing to learn and have enough determination then the first few should be well within your reach. </p>
<p><span id="more-57"></span>One of the fantastic things about Project Euler is that many of the concepts you learn can be used later on as part of a solution to a much more difficult problem. This is a great learning experience and something well worth putting some time into if you&#8217;re interested in this sort of thing.</p>
<h2>Problem 1</h2>
<blockquote><p>
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.</p>
<p>Find the sum of all the multiples of 3 or 5 below 1000.
</p></blockquote>
<p>A very simple first problem to get us started.</p>
<p>Arguably the most obvious solution is a bruteforce algorithm which looks at the numbers 1-999, checks whether they meet the desired criteria (divides evenly by 3 or 5) and returns the sum of the valid results.</p>
<p>Below is my implementation of the algorithm in Python:</p>
<pre class="brush: python;">
sum = 0
for i in xrange(1000):
    if ((i % 3) == 0) or ((i % 5) == 0):
        sum += i
print sum
</pre>
<p>Or in much less (readable) code:</p>
<pre class="brush: python;">print sum(x for x in xrange(1000) if (x % 3) == 0 or (x % 5) == 0)</pre>
<p>Even using bruteforce it computes the answer comfortably inside Project Euler&#8217;s &#8220;one-minute rule&#8221;, it takes only 0.035s to run only my 2.2Ghz laptop.</p>
<p>Of course there&#8217;s more than one way to skin a cat. Another solution is to sum all the multiples of 3 and 5 between 1 and 999 and subtract the sum of the duplicates, in this case all multiples of 15. This method is an example of the <a href="http://en.wikipedia.org/wiki/Inclusion-exclusion_principle">Inclusion-exclusion principle</a>. </p>
<p>Python makes this very easy to do thanks to the range() function (or xrange if your working with larger numbers and have the memory to spare):</p>
<pre class="brush: python;">
print sum(xrange(0,1000,3)) + sum(xrange(0,1000,5)) - sum(xrange(0,1000,15))
</pre>
<p>This runs in approximately the same time as my bruteforce algorithm, there are much more efficient ways of calculating the sum of a series of numbers but I&#8217;ll leave you to discover them for yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://jamesmcminn.com/2009/08/19/project-euler/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
