<?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>thattommyhall.com &#187; euler</title>
	<atom:link href="http://www.thattommyhall.com/category/euler/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thattommyhall.com</link>
	<description>A Random Walk Through Idea Space</description>
	<lastBuildDate>Tue, 20 Jul 2010 19:13:21 +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 39</title>
		<link>http://www.thattommyhall.com/2008/04/12/project-euler-39/</link>
		<comments>http://www.thattommyhall.com/2008/04/12/project-euler-39/#comments</comments>
		<pubDate>Sat, 12 Apr 2008 18:49:57 +0000</pubDate>
		<dc:creator>tom</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[euler]]></category>

		<guid isPermaLink="false">http://www.thattommyhall.com/2008/04/12/project-euler-39/</guid>
		<description><![CDATA[If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120. {20,48,52}, {24,45,51}, {30,40,50} For which value of p &#60; 1000, is the number of solutions maximised? WARNING: CONTAINS MATHEMATICS You may remember from school (Difference of 2 squares) let and so [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.<br />
{20,48,52}, {24,45,51}, {30,40,50}<br />
For which value of p &lt; 1000, is the number of solutions maximised?</p></blockquote>
<p>WARNING: CONTAINS MATHEMATICS<br />
<span id="more-55"></span><br />
You may remember from school<br />
<img src="http://www.codecogs.com/eq.latex?a^2+b^2=c^2" alt="" /><br />
<img src="http://www.codecogs.com/eq.latex?a%5E2%20=%20c%5E2%20-%20b%5E2" alt="" /><br />
<img src="http://www.codecogs.com/eq.latex?a%5E2%20=%20%28c+b%29%28c-b%29" alt="" /><br />
(Difference of 2 squares)<br />
let<br />
<img src="http://www.codecogs.com/eq.latex?m=c+b" alt="" /><br />
and<br />
<img src="http://www.codecogs.com/eq.latex?n=c-b" alt="" /><br />
so<br />
<img src="http://www.codecogs.com/eq.latex?m+n=2c" alt="" /><br />
<img src="http://www.codecogs.com/eq.latex?m-n=2b" alt="" /><br />
and substituting these into <img src="http://www.codecogs.com/eq.latex?a^2=c^2-b^2" alt="" /><br />
gives<br />
<img src="http://www.codecogs.com/eq.latex?a=\sqrt{mn}" alt="" /><br />
Set <img src="http://www.codecogs.com/eq.latex?2p^2=m" alt="" /> and <img src="http://www.codecogs.com/eq.latex?2q^2=n" alt="" /> to get rid of that root and you have<br />
<img src="http://www.codecogs.com/eq.latex?a=2pq" alt="" /><br />
<img src="http://www.codecogs.com/eq.latex?b=p^2-q^2" alt="" /><br />
<img src="http://www.codecogs.com/eq.latex?c=p^2+q^2" alt="" /></p>
<p>Primitive triples are ones in lowest terms (if you know (3,4,5) you can get (6,8,10) etc by multiplying each side by some integer)<br />
If p and q are coprime and different parity you just get primitive ones (i did not bother and just used set() to remove dupes from the list)</p>
<p>Here is my code</p>
<pre class="brush: python;">
from __future__ import division
from collections import defaultdict
from time import time
     start = time()
counter = defaultdict(list)
    def triples():
    for q in range(1, 35):
        #35 is greater than sqrt(1000)
        for p in range(q+1, 35):
            a = p**2 - q**2
            b = 2 * p * q
            c = p**2 + q**2
            for m in range(1,int(1000/c)):
                #generate multiples till c is above 1000
                #triples with perimeter &gt; 1000 are filtered later
                yield (m*min(a,b),m*max(a,b), m*c)
    for triple in triples():
        perimeter = sum(triple)
        if perimeter &lt; 1001:
        counter[perimeter] += [triple]
&lt;p&gt;print set((counter[120]))
&lt;p&gt;maxlen = 1&lt;br /&gt;
for i in counter:
    length = len(set(counter[i]))&lt;br /&gt;
    if length &gt; maxlen:&lt;br /&gt;
        maxlen = length&lt;br /&gt;
        print i&lt;br /&gt;
end = time()&lt;/p&gt;
&lt;p&gt;print &quot;took &quot;, end-start&lt;/p&gt;
&lt;p&gt;</pre>
<p>Which outputs:<br />
<code>set([(24, 45, 51), (20, 48, 52), (30, 40, 50)])<br />
520<br />
528<br />
630<br />
660<br />
720<br />
840<br />
took  0.0140960216522</code></p>
<p>I tried brute forcing it today and knocked up the following</p>
<pre class="brush: python;">&lt;br /&gt;
from collections import defaultdict&lt;br /&gt;
counter = defaultdict(int)&lt;br /&gt;
from time import time&lt;/p&gt;
&lt;p&gt;def run():&lt;br /&gt;
	start = time()&lt;br /&gt;
	for perimeter in range(1,1001):&lt;br /&gt;
		#print perimeter&lt;br /&gt;
		for a in range(1,perimeter):&lt;br /&gt;
			for b in range(1,perimeter-a):&lt;br /&gt;
				c = perimeter - a - b&lt;br /&gt;
				if a**2 + b**2 == c**2:&lt;br /&gt;
					counter[a+b+c] += 1&lt;br /&gt;
	mosttriples = 0&lt;br /&gt;
	for i in counter:&lt;br /&gt;
		if counter[i] &gt; mosttriples:&lt;br /&gt;
			mosttriples = counter[i]&lt;br /&gt;
			print i&lt;br /&gt;
	end = time()&lt;br /&gt;
	print &quot;TOOK&quot;, end-start&lt;/p&gt;
&lt;p&gt;print &quot;without psyco&quot;&lt;br /&gt;
run()&lt;/p&gt;
&lt;p&gt;print &quot;with&quot;&lt;br /&gt;
import psyco&lt;br /&gt;
psyco.full()&lt;br /&gt;
run()&lt;br /&gt;
</pre>
<p>Which gives</p>
<pre class="brush: plain;">without psyco
516
520
528
630
660
720
840
TOOK 134.663383007
with
516
520
528
630
660
720
840
TOOK 21.0988409519
</pre>
<p>So psyco makes it 6 times faster but a smarter algorithm is 2000 times faster still.</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://www.thattommyhall.com/2008/04/12/project-euler-39/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.thattommyhall.com/2008/04/12/project-euler-39/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
