<?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; Triangles</title>
	<atom:link href="http://blog.dreamshire.com/tag/triangles/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 75 Solution</title>
		<link>http://blog.dreamshire.com/2009/06/08/project-euler-problem-75-solution/</link>
		<comments>http://blog.dreamshire.com/2009/06/08/project-euler-problem-75-solution/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 21:14:57 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 70-79]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[Triangles]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1570</guid>
		<description><![CDATA[Find the number of different lengths of wire can that can form a right angle triangle in only one way.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>It turns out that 12 cm is the smallest length of wire that can be bent to form an integer sided right angle triangle in exactly one way, but there are many more examples.</p>
<p style='margin-left:50px;'><b>12 cm</b>: (3,4,5)<br />
<b>24 cm</b>: (6,8,10)<br />
<b>30 cm</b>: (5,12,13)<br />
<b>36 cm</b>: (9,12,15)<br />
<b>40 cm</b>: (8,15,17)<br />
<b>48 cm</b>: (12,16,20)</p>
<p>In contrast, some lengths of wire, like 20 cm, cannot be bent to form an integer sided right angle triangle, and other lengths allow more than one solution to be found; for example, using 120 cm it is possible to form exactly three different integer sided right angle triangles.</p>
<p style='margin-left:50px;'><b>120 cm</b>: (30,40,50), (20,48,52), (24,45,51)</p>
<p>Given that L is the length of the wire, for how many values of L &le; 2,000,000 can exactly one integer sided right angle triangle be formed?</p>
<h4><u>Analysis</u></h4>
<p>This is an extension to the <a href="http://blog.dreamshire.com/2009/05/09/project-euler-problem-9-solution/">Problem 9</a> solution.  </p>
<h4><u>Solution</u></h4>
<p>Runs < 3 seconds in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> Euler <span style="color: #ff7700;font-weight:bold;">import</span> gcd
&nbsp;
limit = <span style="color: #ff4500;">2000000</span>
sqrt_limit = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>limit<span style="color: #66cc66;">**</span>.5<span style="color: black;">&#41;</span>
tx = <span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">*</span>limit
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, sqrt_limit, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, sqrt_limit, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> gcd<span style="color: black;">&#40;</span>i, j<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">1</span>: 
      <span style="color: #008000;">sum</span> = <span style="color: #008000;">abs</span><span style="color: black;">&#40;</span>j<span style="color: #66cc66;">*</span>j - i<span style="color: #66cc66;">*</span>i<span style="color: black;">&#41;</span> + <span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>i<span style="color: #66cc66;">*</span>j + i<span style="color: #66cc66;">*</span>i + j<span style="color: #66cc66;">*</span>j
      <span style="color: #ff7700;font-weight:bold;">for</span> s <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">sum</span>, limit, <span style="color: #008000;">sum</span><span style="color: black;">&#41;</span>:
        tx<span style="color: black;">&#91;</span>s<span style="color: black;">&#93;</span>+=<span style="color: #ff4500;">1</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE75 = &quot;</span>, tx.<span style="color: black;">count</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span></pre></div></div>

<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/06/08/project-euler-problem-75-solution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 39 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/22/project-euler-problem-39-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/22/project-euler-problem-39-solution/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 19:25:49 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 30-39]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[Triangles]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=860</guid>
		<description><![CDATA[If p is the perimeter of a right angle triangle, {a, b, c}, which value, for p ≤ 1000, has the most solutions?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>If <i>p</i> is the perimeter of a right angle triangle with integral length sides, {<i>a</i>,<i>b</i>,<i>c</i>}, there are exactly three solutions for <i>p</i> = 120.</p>
<p style='text-align:center;'>{20,48,52}, {24,45,51}, {30,40,50}</p>
<p>For which value of <i>p</i> &le; 1000, is the number of solutions maximised?</p>
<h4><u>Analysis</u></h4>
<p>The Pythagorean Theorem gives us: a<sup>2</sup> + b<sup>2</sup> = c<sup>2</sup> and by intuition a + b + c = <em>p</em> (perimeter).<br />
The decision to iterate perimeters has many advantages over generating right triangles.  Only even numbered perimeters need to be checked; below is our analysis:</p>
<table>
<tr>
<th colspan="2">Independent</th>
<td width=5> </td>
<th colspan="2">Dependent</th>
</tr>
<tr>
<th>a</th>
<th>b</th>
<td width=5> </td>
<th>c</th>
<th><em>p</em></th>
</tr>
<tr>
<td>E</td>
<td>E</td>
<td width=5> </td>
<td>E</td>
<td>E</td>
</tr>
<tr>
<td>O</td>
<td>O</td>
<td width=5> </td>
<td>E</td>
<td>E</td>
</tr>
<tr>
<td>E</td>
<td>O</td>
<td width=5> </td>
<td>O</td>
<td>E</td>
</tr>
<tr>
<td>O</td>
<td>E</td>
<td width=5> </td>
<td>E</td>
<td>E</td>
</tr>
</table>
<p>No matter which integral values we choose for a, b and c such that a<sup>2</sup> + b<sup>2</sup> = c<sup>2</sup> the perimeter will be even.</p>
<p>By iterating perimeters we need a method to check for integral (integer) right triangles.<br />
We can interpret the Pythagorean theorem as a<sup>2</sup> + b<sup>2</sup> = (<em>p</em>-a-b)<sup>2</sup> since <em>c</em> = <em>p</em>-<em>a</em>-<em>b</em>.  After expanding and simplifying we have:<br />
<em>b</em> = <em>p</em>(<em>p</em> &#8211; 2a) / 2(<em>p</em>-a) and when this equation evaluates to an integer (MOD(<em>p</em>(<em>p</em> &#8211; 2a), 2(<em>p</em>-a)) = 0) we have our triangle.</p>
<p>One last optimization.  By using a+b>c and symmetry we only need to investigate values of <em>a</em> to <em>p</em>/4.</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;">t_max = <span style="color: #ff4500;">0</span><span style="color: #66cc66;">;</span>
p_limit = <span style="color: #ff4500;">1000</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> p <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, p_limit+<span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
  t = <span style="color: #ff4500;">0</span><span style="color: #66cc66;">;</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> a <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, p/<span style="color: #ff4500;">4</span>+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span>  p<span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span>p - <span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>a<span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span>p-a<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span>: t += <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> t <span style="color: #66cc66;">&gt;</span> t_max: <span style="color: black;">&#40;</span>t_max, p_max<span style="color: black;">&#41;</span> = <span style="color: black;">&#40;</span>t, p<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE30 = &quot;</span>, p_max</pre></div></div>

<h4><u>Comments</u></h4>
<p>Some other results:<br />
10,000 = 9,240<br />
100,000 = 55,440<br />
1,000,000 = 720,720<br />
10,000,000 = 6,126,120</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/22/project-euler-problem-39-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
