<?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; Inclusion-Exclusion Principle</title>
	<atom:link href="http://blog.dreamshire.com/tag/inclusion-exclusion-principle/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 1 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/17/project-euler-problem-1-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/17/project-euler-problem-1-solution/#comments</comments>
		<pubDate>Mon, 18 May 2009 00:45:05 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 1-9]]></category>
		<category><![CDATA[Inclusion-Exclusion Principle]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1236</guid>
		<description><![CDATA[Add all the natural numbers below one thousand that are multiples of 3 or 5.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<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>
<h4><u>Analysis</u></h4>
<p>This is an ideal solution for the <a href="http://en.wikipedia.org/wiki/Inclusion-exclusion_principle">inclusion-exclusion principle</a>.  In general, we sum the numbers divisible by 3 or 5 for all numbers less than n, where n=1000 for this problem.  Next, we would need to subtracxt those numbers divisible by 15 because they are counted twice.</p>
<p>This solution is very extensible instead of using brute force that requires loops.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> sumn<span style="color: black;">&#40;</span>n,d<span style="color: black;">&#41;</span>:
  n=<span style="color: #008000;">int</span><span style="color: black;">&#40;</span>n/d<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>n<span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span>n+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> / <span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>d<span style="color: black;">&#41;</span>
&nbsp;
n = <span style="color: #ff4500;">999</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE1 = &quot;</span>,sumn<span style="color: black;">&#40;</span>n,<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span> + sumn<span style="color: black;">&#40;</span>n,<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span> - sumn<span style="color: black;">&#40;</span>n,<span style="color: #ff4500;">15</span><span style="color: black;">&#41;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>The formula to sum the counting numbers from 1 to n is: n(n+1)/2.  As for n=10; 10*11/2=55</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/17/project-euler-problem-1-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
