<?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>Geekality &#187; Pythagorean triplets</title>
	<atom:link href="http://www.geekality.net/tag/pythagorean-triplets/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.geekality.net</link>
	<description>With a hint of Social Ineptitude</description>
	<lastBuildDate>Tue, 27 Jul 2010 16:15:56 +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 9</title>
		<link>http://www.geekality.net/2009/09/24/project-euler-problem-9/</link>
		<comments>http://www.geekality.net/2009/09/24/project-euler-problem-9/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 17:53:31 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Pythagorean triplets]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=512</guid>
		<description><![CDATA[A Pythagorean triplet is a set of three natural numbers, for which, For example, There exists exactly one Pythagorean triplet for which, Find the product abc. Solution This I wasn&#8217;t quite sure how to solve effectively and my solution is &#8230; <a href="http://www.geekality.net/2009/09/24/project-euler-problem-9/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<blockquote><p>A Pythagorean triplet is a set of three natural numbers, for which,</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_e5aa9961f21bfa7dc1c3483bd29bd4e6.gif" alt="a < b < c \qquad a^2 + b^2 = c^2" title="a < b < c \qquad a^2 + b^2 = c^2" style="vertical-align: -1px; border: none;"/></p></blockquote>
<p>For example,</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_bdaa8c3f8e937ce1c816c82530e37d50.gif" alt="3^2 + 4^2 = 9 + 16 = 25 = 5^2" title="3^2 + 4^2 = 9 + 16 = 25 = 5^2" style="vertical-align: -1px; border: none;"/></p></blockquote>
<p>There exists exactly one Pythagorean triplet for which,</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_6bad5dd3a7b97b75f0b55e93e8067d01.gif" alt="a + b + c = 1000" title="a + b + c = 1000" style="vertical-align: -1px; border: none;"/></p></blockquote>
<p>Find the product abc.</p></blockquote>
<p><span id="more-512"></span></p>
<h2>Solution</h2>
<p>This I wasn&#8217;t quite sure how to solve effectively and my solution is probably not the fastest or most clever, but at least it is pretty simple <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> To start off, we need a source of Pythagorean triplets.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> PythagoreanTriples <br />
&nbsp; &nbsp; <span style="color: #008000;">:</span> IEnumerable<span style="color: #008000;">&lt;</span>Triple<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ulong</span>, <span style="color: #6666cc; font-weight: bold;">ulong</span>, <span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&gt;&gt;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> IEnumerator<span style="color: #008000;">&lt;</span>Triple<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ulong</span>, <span style="color: #6666cc; font-weight: bold;">ulong</span>, <span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&gt;&gt;</span> GetEnumerator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span> c <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;;</span> c<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span> b <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> b <span style="color: #008000;">&lt;=</span> c<span style="color: #008000;">;</span> b<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span> a <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> a <span style="color: #008000;">&lt;</span> b<span style="color: #008000;">;</span> a<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>a<span style="color: #008000;">*</span>a<span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #008000;">&#40;</span>b<span style="color: #008000;">*</span>b<span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> c<span style="color: #008000;">*</span>c<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> Tuple<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">From</span><span style="color: #008000;">&#40;</span>a, b, c<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; IEnumerator IEnumerable<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEnumerator</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> GetEnumerator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p><span class="my-note"><span class="my-note-heading">Note:</span> You can find the <a href="http://abdullin.com/journal/2008/9/30/using-tuples-in-c-functions-and-dictionaries.html">Triple</a> class in the <a href="http://abdullin.com/shared-libraries/">Lokad Shared Libraries</a>.</span></p>
<p>And that&#8217;s pretty much what we need to solve this problem actually.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var t <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PythagoreanTriples<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">First</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Item1</span> <span style="color: #008000;">+</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Item2</span> <span style="color: #008000;">+</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Item3</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">1000</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
var answer <span style="color: #008000;">=</span> t<span style="color: #008000;">.</span><span style="color: #0000FF;">Item1</span> <span style="color: #008000;">*</span> t<span style="color: #008000;">.</span><span style="color: #0000FF;">Item2</span> <span style="color: #008000;">*</span> t<span style="color: #008000;">.</span><span style="color: #0000FF;">Item3</span><span style="color: #008000;">;</span></div></div>
<p>This piece of code simply goes through triplets until it finds the first one fulfilling our requirement. It runs in around 230 milliseconds, which is acceptable enough.</p>
<p>Do you have a more clever way to do this? Please leave a comment and share <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/09/24/project-euler-problem-9/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
