<?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; Mathematics</title>
	<atom:link href="http://www.thattommyhall.com/category/mathematics/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thattommyhall.com</link>
	<description>A Random Walk Through Idea Space</description>
	<lastBuildDate>Sun, 08 Jan 2012 11:42:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Infinite Prime Number Generator In Python</title>
		<link>http://www.thattommyhall.com/2010/10/18/prime-gen/</link>
		<comments>http://www.thattommyhall.com/2010/10/18/prime-gen/#comments</comments>
		<pubDate>Mon, 18 Oct 2010 12:50:35 +0000</pubDate>
		<dc:creator>tom</dc:creator>
				<category><![CDATA[euler]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.thattommyhall.com/?p=321</guid>
		<description><![CDATA[I was looking at this article (The Genuine Sieve of Eratosthenes / PDF) about a common functional prime number generator that is mistakenly called the Sieve of Eratosthenes. It is quite complicated, section 2 deals with the performance of the naive algorithm and 3 implements the sieve properly. I could not quite grok the Haskell [...]]]></description>
			<content:encoded><![CDATA[<p>I was looking at this article (<a href="http://lambda-the-ultimate.org/node/3127">The Genuine Sieve of Eratosthenes</a> / <a href="http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf">PDF</a>) about a common functional prime number generator that is mistakenly called the Sieve of Eratosthenes.</p>
<p>It is quite complicated, section 2 deals with the performance of the naive algorithm and 3 implements the sieve properly. I could not quite grok the Haskell but the following quote helped me know what was going on </p>
<blockquote><p>Whereas the original algorithm crosses off all multiples of a prime at once, we perform these “crossings off” in a lazier way: crossing off just-in-time. For this purpose, we will store a table in which, for each prime p that we have discovered so far, there is an “iterator” holding the next multiple of p to cross off. Thus, instead of crossing off all the multiples of, say, 17, at once (impossible, since there are infinitely many for our limit-free algorithm), we will store the first one (at 17 × 17; i.e., 289) in our table of upcoming composite numbers. When we come to consider whether 289 is prime, we will check our composites table and discover that it is a known composite with 17 as a factor, remove 289 from the table, and insert 306 (i.e., 289+17). <strong>In essence, we are storing “iterators” in a table keyed by the current value of each iterator.</strong></p></blockquote>
<p>Here it is using a dictionary for each composite number with a list of increments it was reached by:</p>
<pre class="brush: python; title: ; notranslate">
from itertools import count

def gen_primes():
    yield 2
    def update_composites(number,increment):
        try:
            composites[number] += [increment]
        except:
            composites[number] = [increment]

    composites = {4:[2]}

    c = count(3)
    while 1:
        #print composites
        next = c.next()
        smallest = min(composites.keys())
        incs = composites[smallest]
        del composites[smallest]
        for inc in incs:
            update_composites(smallest+inc,inc)
        if next &lt; smallest:
            yield next
            c.next()
            update_composites(next**2,next)

g=gen_primes()

for i in range(100):
    print g.next()
</pre>
<p>(On <a href="http://github.com/thattommyhall/Project-Euler/blob/master/PrimeGen.py">Github</a>)</p>
<p>They mention a speedup moving to a <a href="http://docs.python.org/library/heapq.html#theory">heapqueue</a> as the data structure (as we just take the lowest each lookup) so I implemented that too, here is the code</p>
<pre class="brush: python; title: ; notranslate">
from itertools import count
from heapq import heappush,heappop

def gen_primes():
    yield 2

    def update_composites(number,increment):
        heappush(composites,(number,increment))
    composites = [(4,2)]

    c = count(3)

    while 1:
        #print composites
        smallest,increment = heappop(composites)
        update_composites(smallest+increment,increment)
        if composites[0][0] == smallest:
            continue
        next = c.next()
        if next &lt; smallest:
            yield next
            c.next()
            update_composites(next**2,next)

g=gen_primes()

for i in range(100):
    print g.next()
</pre>
<p>(On <a href="http://github.com/thattommyhall/Project-Euler/blob/master/PrimeGen2.py">github</a>)</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://www.thattommyhall.com/2010/10/18/prime-gen/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.thattommyhall.com/2010/10/18/prime-gen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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[euler]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Python]]></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; title: ; notranslate">
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; title: ; notranslate">&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; title: ; notranslate">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>
		<item>
		<title>Arithmetic is a great bollocks detector</title>
		<link>http://www.thattommyhall.com/2008/03/29/arithmetic-is-a-great-bollocks-detector/</link>
		<comments>http://www.thattommyhall.com/2008/03/29/arithmetic-is-a-great-bollocks-detector/#comments</comments>
		<pubDate>Sat, 29 Mar 2008 15:29:48 +0000</pubDate>
		<dc:creator>tom</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[rant]]></category>

		<guid isPermaLink="false">http://www.thattommyhall.com/2008/03/29/arithmetic-is-a-great-bollocks-detector/</guid>
		<description><![CDATA[Go and read Pupils to get &#8216;new world&#8217; trips from the beeb. You may think &#8220;trips abroad for kids, great&#8221; , if you are more cynical you may think &#8220;heads choose the kids, not sure I like that&#8221;. Let us do some arithmetic, 100 kids for 6 weeks each for £1.4M. £1400000/100 = £14,000 per [...]]]></description>
			<content:encoded><![CDATA[<p>Go and read <a href="http://news.bbc.co.uk/1/hi/education/7168598.stm" target="_blank">Pupils to get &#8216;new world&#8217; trips</a> from the beeb.</p>
<p>You may think &#8220;trips abroad for kids, great&#8221; , if you are more cynical you may think &#8220;heads choose the kids, not sure I like that&#8221;.</p>
<p>Let us do some arithmetic, 100 kids for 6 weeks each for £1.4M.</p>
<ul>
<li>£1400000/100 = £14,000 per child</li>
<li>£14000/6 = £2&#8217;333 per week</li>
<li>Assuming a 40 hour working week, £58/hour</li>
</ul>
<p>Of course its not that simple, the kids clearly don&#8217;t get all the money as if they had a 6 week job. I think a few marketeers in the UK, all the necessary admin by the British Council will do away with some of the money too, but I can&#8217;t imagine any way this is good value for the country or any organisation involved (I am not cynical enough to suggest that for the BC <em>the point is to administer it</em>.)</p>
<p>Hows about giving 1000 kids £1000 spend in a country of their choice, you could get them to bid for the money and report back with diaries and photos etc. All the £500&#8242;s the government paid into child trust funds for children with low-income parents could become a good jolly fund for them when they hit 18, or they could get driving lessons and a car &#8211; not a bad deal, particularly if their folks contribute anything else to the fund along the way. That or 18th birthday parties become 5 day benders in estates around Britain.</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://www.thattommyhall.com/2008/03/29/arithmetic-is-a-great-bollocks-detector/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.thattommyhall.com/2008/03/29/arithmetic-is-a-great-bollocks-detector/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>You Know You Are A Maths Geek When&#8230;</title>
		<link>http://www.thattommyhall.com/2008/02/22/you-know-you-are-a-maths-geek-when/</link>
		<comments>http://www.thattommyhall.com/2008/02/22/you-know-you-are-a-maths-geek-when/#comments</comments>
		<pubDate>Fri, 22 Feb 2008 00:57:27 +0000</pubDate>
		<dc:creator>tom</dc:creator>
				<category><![CDATA[Mathematics]]></category>

		<guid isPermaLink="false">http://www.thattommyhall.com/2008/02/22/you-know-you-are-a-maths-geek-when/</guid>
		<description><![CDATA[You are walking home late at night and two girls run past, one turns to the other and says &#8220;this is the millionth time I have ran today&#8221; and you cannot help but say &#8220;You would have to start and stop running 10 times a second, while you said that you would have to have [...]]]></description>
			<content:encoded><![CDATA[<p>You are walking home late at night and two girls run past, one turns to the other and says &#8220;this is the millionth time I have ran today&#8221; and you cannot help but say &#8220;You would have to start and stop running 10 times a second, while you said that you would have to have done it 30 times&#8221;</p>
<p>You know its real bad when you get home, do the calculation and and are disappointed you did not say 11.5 and then blog about it.</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://www.thattommyhall.com/2008/02/22/you-know-you-are-a-maths-geek-when/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.thattommyhall.com/2008/02/22/you-know-you-are-a-maths-geek-when/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>You know you&#8217;re a maths geek when..</title>
		<link>http://www.thattommyhall.com/2007/11/21/you-know-youre-a-maths-geek-when/</link>
		<comments>http://www.thattommyhall.com/2007/11/21/you-know-youre-a-maths-geek-when/#comments</comments>
		<pubDate>Wed, 21 Nov 2007 14:00:11 +0000</pubDate>
		<dc:creator>tom</dc:creator>
				<category><![CDATA[Mathematics]]></category>

		<guid isPermaLink="false">http://www.thattommyhall.com/2007/11/21/you-know-youre-a-maths-geek-when/</guid>
		<description><![CDATA[You are singing the song that goes &#8220;I got love for you if you were born in the 80s&#8221; and your girlfriend (who was born in 1979) says &#8220;what, no love for me?&#8221; and you reply &#8220;actually what I said does not imply that at all, you cant just negate both sides of a proposition [...]]]></description>
			<content:encoded><![CDATA[<p>You are singing the song that goes &#8220;I got love for you if you were born in the 80s&#8221; and your girlfriend (who was born in 1979) says &#8220;what, no love for me?&#8221; and you reply &#8220;actually what I said does not imply that at all, you cant just negate both sides of a proposition and expect to get a true statement, if you reverse the implication too (to get the contrapositive) then you have a true statement, ie if I don&#8217;t love you you weren&#8217;t born in the 80s.&#8221;</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://www.thattommyhall.com/2007/11/21/you-know-youre-a-maths-geek-when/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.thattommyhall.com/2007/11/21/you-know-youre-a-maths-geek-when/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tommys Project</title>
		<link>http://www.thattommyhall.com/2007/11/15/tommys-project/</link>
		<comments>http://www.thattommyhall.com/2007/11/15/tommys-project/#comments</comments>
		<pubDate>Thu, 15 Nov 2007 19:00:41 +0000</pubDate>
		<dc:creator>tom</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.thattommyhall.com/2007/11/15/tommys-project/</guid>
		<description><![CDATA[I posted last time about TeXmacs and remembered the plugins for using it to display output from some of the superb free maths packages (which sometimes lack a nice display). The one I am excited about most is for SAGE. SAGE is written in python and leverages the hard work of the other projects (its [...]]]></description>
			<content:encoded><![CDATA[<p>I posted last time about TeXmacs and remembered the plugins for using it to display output from some of the superb free maths packages (which sometimes lack a nice display).</p>
<p>The one I am excited about most is for <a href="http://www.sagemath.org/index.html" target="_blank">SAGE</a>. SAGE is written in python and leverages the hard work of the other projects (its motto is <em>Building the Car Instead of Reinventing the Wheel</em>).  See the TeXmacs plugin <a href="http://www.sagemath.org:9001/TeXmacs" target="_blank">here</a> and some screenshots of SAGE <a href="http://www.sagemath.org/screen_shots/" target="_blank">here</a>.</p>
<p>I will take a look at the code and see if I can contribute anything, it will be nice to get involved in a big project.</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://www.thattommyhall.com/2007/11/15/tommys-project/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.thattommyhall.com/2007/11/15/tommys-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pointless Testing</title>
		<link>http://www.thattommyhall.com/2007/11/13/pointless-testing/</link>
		<comments>http://www.thattommyhall.com/2007/11/13/pointless-testing/#comments</comments>
		<pubDate>Tue, 13 Nov 2007 13:48:22 +0000</pubDate>
		<dc:creator>tom</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[Mathematics]]></category>

		<guid isPermaLink="false">http://www.thattommyhall.com/2007/11/13/pointless-testing/</guid>
		<description><![CDATA[Usually I am in favour of testing, when people say &#8220;weighing a cow does not make it any heavier&#8221; I reply that if you measure nothing, you will never see any improvements. Usually people can come up with cogent arguments against the particular things examined but it never seems to be the case that measuring [...]]]></description>
			<content:encoded><![CDATA[<p>Usually I am in favour of testing, when people say &#8220;weighing a cow does not make it any heavier&#8221; I reply that if you measure nothing, you will never see any improvements. Usually people can come up with cogent arguments against the particular things examined but it never seems to be the case that measuring nothing is the answer, some better metric needs to be created.</p>
<p>I stumbled across a site that reminded me of when I used to read tomshardware to see how fast new PC parts really were (or look at nice graphs and read a conclusion telling me). Their benchmarks of <a href="http://www.phoronix.com/scan.php?page=article&amp;item=912&amp;num=2" target="_blank">Ubuntu vs Fedora</a> and <a href="http://www.phoronix.com/scan.php?page=article&amp;item=893&amp;num=4" target="_blank">The Last 12 Linux Kernels</a> strike me as pretty pointless. Pages of graphs saying things are the same bar noise, as you would probably expect. The conclusion of the distro shootout at least says you should (could?) not base your decision on the results. The Linux one is interesting:</p>
<blockquote><p>The only benchmark where there was a definitive improvement was with the network performance. Granted, these 12  Linux kernels were only tested on one system and in eight different benchmarks.  We will continue benchmarking the Linux kernel in different environments and report  back once we have any new findings.</p></blockquote>
<p>Of all the benchmarks, I would have predicted in advance that the network one would be noisiest. I would suggest that you would get different results on different machines, perhaps they should use different clocks too.</p>
<p>Do these people creating the graphs know about repeating measurements, averaging and statistical significance tests?  I think not, so what is the point?</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://www.thattommyhall.com/2007/11/13/pointless-testing/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.thattommyhall.com/2007/11/13/pointless-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

