<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Pythonism</title>
	<atom:link href="http://pythonism.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://pythonism.wordpress.com</link>
	<description>joy in simplicity - platonic world of code and math</description>
	<lastBuildDate>Wed, 30 Dec 2009 17:45:32 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='pythonism.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/2a826a81cec782cc5afc7ffea36f68a4?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Pythonism</title>
		<link>http://pythonism.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://pythonism.wordpress.com/osd.xml" title="Pythonism" />
		<item>
		<title>more one liners</title>
		<link>http://pythonism.wordpress.com/2009/12/30/more-one-liners/</link>
		<comments>http://pythonism.wordpress.com/2009/12/30/more-one-liners/#comments</comments>
		<pubDate>Wed, 30 Dec 2009 17:45:32 +0000</pubDate>
		<dc:creator>pythonisms</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[one-liners.triangular]]></category>
		<category><![CDATA[tetrahedral]]></category>

		<guid isPermaLink="false">http://pythonism.wordpress.com/?p=591</guid>
		<description><![CDATA[Triangular numbers one liner
&#62;&#62;&#62; [sum(range(1,n)) for n in range(1,20)]
[0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171]
&#62;&#62;&#62;
and the tetrahedral numbers
&#62;&#62;&#62; [sum([sum(range(1,n)) for n in range(1,m)]) for m in range(2,20)]
[0, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286, 364, 455, 560, 680, 816, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonism.wordpress.com&blog=3580594&post=591&subd=pythonism&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Triangular numbers one liner</p>
<p><code>&gt;&gt;&gt; [sum(range(1,n)) for n in range(1,20)]<br />
[0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171]<br />
&gt;&gt;&gt;</code></p>
<p>and the tetrahedral numbers</p>
<p><code>&gt;&gt;&gt; [sum([sum(range(1,n)) for n in range(1,m)]) for m in range(2,20)]<br />
[0, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286, 364, 455, 560, 680, 816, 969]<br />
&gt;&gt;&gt;</code></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pythonism.wordpress.com/591/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pythonism.wordpress.com/591/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pythonism.wordpress.com/591/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pythonism.wordpress.com/591/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pythonism.wordpress.com/591/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pythonism.wordpress.com/591/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pythonism.wordpress.com/591/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pythonism.wordpress.com/591/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pythonism.wordpress.com/591/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pythonism.wordpress.com/591/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonism.wordpress.com&blog=3580594&post=591&subd=pythonism&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://pythonism.wordpress.com/2009/12/30/more-one-liners/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a5395c548819c89c914016d32e44394a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pythonisms</media:title>
		</media:content>
	</item>
		<item>
		<title>one liners of mathematical interest</title>
		<link>http://pythonism.wordpress.com/2009/12/30/one-liners-of-mathematical-interest/</link>
		<comments>http://pythonism.wordpress.com/2009/12/30/one-liners-of-mathematical-interest/#comments</comments>
		<pubDate>Wed, 30 Dec 2009 17:25:38 +0000</pubDate>
		<dc:creator>pythonisms</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[one-liner]]></category>

		<guid isPermaLink="false">http://pythonism.wordpress.com/?p=585</guid>
		<description><![CDATA[&#62;&#62;&#62; sum([int(a)**6 for a in '548834'])
548834
&#62;&#62;&#62;
short but interesting
       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonism.wordpress.com&blog=3580594&post=585&subd=pythonism&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><code>&gt;&gt;&gt; sum([int(a)**6 for a in '548834'])<br />
548834<br />
&gt;&gt;&gt;</code></p>
<p>short but interesting</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pythonism.wordpress.com/585/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pythonism.wordpress.com/585/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pythonism.wordpress.com/585/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pythonism.wordpress.com/585/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pythonism.wordpress.com/585/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pythonism.wordpress.com/585/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pythonism.wordpress.com/585/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pythonism.wordpress.com/585/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pythonism.wordpress.com/585/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pythonism.wordpress.com/585/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonism.wordpress.com&blog=3580594&post=585&subd=pythonism&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://pythonism.wordpress.com/2009/12/30/one-liners-of-mathematical-interest/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a5395c548819c89c914016d32e44394a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pythonisms</media:title>
		</media:content>
	</item>
		<item>
		<title>digit-reversed products</title>
		<link>http://pythonism.wordpress.com/2009/12/30/digit-reversed-products/</link>
		<comments>http://pythonism.wordpress.com/2009/12/30/digit-reversed-products/#comments</comments>
		<pubDate>Wed, 30 Dec 2009 16:46:23 +0000</pubDate>
		<dc:creator>pythonisms</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[number]]></category>
		<category><![CDATA[product]]></category>
		<category><![CDATA[reversal]]></category>

		<guid isPermaLink="false">http://pythonism.wordpress.com/?p=582</guid>
		<description><![CDATA[def&#160;flip(a):
&#160;&#160;&#160;&#160;return&#160;int(str(a)[::-1])

out=[]
for&#160;a&#160;in&#160;range(10,100):
&#160;&#160;&#160;&#160;for&#160;b&#160;in&#160;range(10,100):
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if&#160;a*b&#160;==&#160;flip(a)*flip(b)&#160;and&#160;a!=&#160;flip(a)&#160;and&#160;b!=flip(b)&#160;and&#160;a!=flip(b)&#160;and&#160;b!=flip(a):
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;out.append([a*b,a,b])

out.sort()
out2=[]
for&#160;x&#160;in&#160;out:
&#160;&#160;&#160;&#160;if&#160;x[0]&#160;not&#160;in&#160;out2:
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;out2.append(x[0])
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;print&#160;x

&#62;&#62;&#62;
[504, 12, 42]
[756, 12, 63]
[806, 13, 62]
[1008, 12, 84]
[1148, 14, 82]
[1209, 13, 93]
[1472, 23, 64]
[1512, 24, 63]
[2016, 24, 84]
[2208, 23, 96]
[2418, 26, 93]
[2924, 34, 86]
[3024, 36, 84]
[4416, 46, 96]
&#62;&#62;&#62;
       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonism.wordpress.com&blog=3580594&post=582&subd=pythonism&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><code>def&nbsp;flip(a):<br />
&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;int(str(a)[::-1])<br />
</code><code><br />
out=[]<br />
for&nbsp;a&nbsp;in&nbsp;range(10,100):<br />
&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;b&nbsp;in&nbsp;range(10,100):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;a*b&nbsp;==&nbsp;flip(a)*flip(b)&nbsp;and&nbsp;a!=&nbsp;flip(a)&nbsp;and&nbsp;b!=flip(b)&nbsp;and&nbsp;a!=flip(b)&nbsp;and&nbsp;b!=flip(a):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.append([a*b,a,b])<br />
</code><code><br />
out.sort()<br />
out2=[]<br />
for&nbsp;x&nbsp;in&nbsp;out:<br />
&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;x[0]&nbsp;not&nbsp;in&nbsp;out2:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out2.append(x[0])<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;x</code></p>
<p><code><br />
&gt;&gt;&gt;<br />
[504, 12, 42]<br />
[756, 12, 63]<br />
[806, 13, 62]<br />
[1008, 12, 84]<br />
[1148, 14, 82]<br />
[1209, 13, 93]<br />
[1472, 23, 64]<br />
[1512, 24, 63]<br />
[2016, 24, 84]<br />
[2208, 23, 96]<br />
[2418, 26, 93]<br />
[2924, 34, 86]<br />
[3024, 36, 84]<br />
[4416, 46, 96]<br />
&gt;&gt;&gt;</code></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pythonism.wordpress.com/582/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pythonism.wordpress.com/582/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pythonism.wordpress.com/582/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pythonism.wordpress.com/582/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pythonism.wordpress.com/582/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pythonism.wordpress.com/582/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pythonism.wordpress.com/582/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pythonism.wordpress.com/582/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pythonism.wordpress.com/582/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pythonism.wordpress.com/582/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonism.wordpress.com&blog=3580594&post=582&subd=pythonism&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://pythonism.wordpress.com/2009/12/30/digit-reversed-products/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a5395c548819c89c914016d32e44394a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pythonisms</media:title>
		</media:content>
	</item>
		<item>
		<title>CSS Color Palette Randomiser</title>
		<link>http://pythonism.wordpress.com/2009/12/05/css-color-palette-randomiser/</link>
		<comments>http://pythonism.wordpress.com/2009/12/05/css-color-palette-randomiser/#comments</comments>
		<pubDate>Sat, 05 Dec 2009 10:10:05 +0000</pubDate>
		<dc:creator>pythonisms</dc:creator>
				<category><![CDATA[internet programming]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[palette]]></category>
		<category><![CDATA[colour]]></category>
		<category><![CDATA[color]]></category>
		<category><![CDATA[randomiser]]></category>

		<guid isPermaLink="false">http://pythonism.wordpress.com/?p=572</guid>
		<description><![CDATA[After spending some time thinking about the web design process I came up with an idea that is relatively simple (that&#8217;s us) to implement in Python. It accelerates the process of coming up with colour schemes for websites by generating a load of pages each with different colors for the text and box background. These [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonism.wordpress.com&blog=3580594&post=572&subd=pythonism&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>After spending some time thinking about the web design process I came up with an idea that is relatively simple (that&#8217;s us) to implement in Python. It accelerates the process of coming up with colour schemes for websites by generating a load of pages each with different colors for the text and box background. These can then be viewed and nice color combinations can be saved for later.</p>
<p>All the code does is to take a starting page with an external stylesheet and makes numbered copies each with different hex values for the colours in the stylesheet. Each html page is named with a number, eg &#8220;26.html&#8221; and the corresponding stylesheet has the same number, as 26.css. It makes it easier to sift through lots of pages since they are in series and each page contains a link at the bottom which links to the next in the series.</p>
<p>The code chooses from all ~16 million possible hex colors, and thus there is an immense combinatorial explosion yielding a truly scary number of possible palettes. It has to be said that there are many more unsuitable palettes generated than good ones, but every now and then you see one that works and it seeds an idea.</p>
<p>Each color entry in the starting stylesheet is initially set to &#8220;color: white;&#8221; and the code replaces every one of these with a &#8220;color: #12aa12&#8243; entry subsituting a random hex value. If you want to breed colors from combinations you like then you can edit the starting stylesheet and add colors you have found that you like, since it only replaces white it won&#8217;t touch your added colors and so you can slowly evolve a nice palette through random mutation.</p>
<p>After talking to a knowledgeable friend I gather this can be done with PHP quite easily too.</p>
<p>I used a page I had from another site I made but you can easily modify the code to run using your own html/css combination.</p>
<p><code><br />
from&nbsp;random&nbsp;import&nbsp;*<br />
colors=[]<br />
for&nbsp;h&nbsp;in&nbsp;range(256):<br />
&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;i&nbsp;in&nbsp;range(256):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;j&nbsp;in&nbsp;range(256):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;color='#'+str(hex(h))[-2:]+str(hex(i))[-2:]+str(hex(j))[-2:]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;colors.append(color)<br />
a=open("test1.html").readlines()<br />
style=open("style.css").readlines()<br />
for&nbsp;zz&nbsp;in&nbsp;range(1,200):<br />
&nbsp;&nbsp;&nbsp;&nbsp;b=open(str(zz)+'.html','w')<br />
&nbsp;&nbsp;&nbsp;&nbsp;b.write('&lt;html&gt;&lt;head&gt;')<br />
&nbsp;&nbsp;&nbsp;&nbsp;b.write('&lt;link&nbsp;type="text/css"&nbsp;rel="stylesheet"&nbsp;href="'+str(zz)+'.css"&gt;')<br />
&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;line&nbsp;in&nbsp;a:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b.write(line)<br />
&nbsp;&nbsp;&nbsp;&nbsp;b.write('&lt;h1&gt;&lt;a&nbsp;href="'+str(zz+1)+'.html"&gt;next&lt;/a&gt;&lt;/h1&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;b.write('&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;')<br />
&nbsp;&nbsp;&nbsp;&nbsp;b.close()<br />
for&nbsp;xx&nbsp;in&nbsp;range(1,200):<br />
&nbsp;&nbsp;&nbsp;&nbsp;c=open(str(xx)+'.css','w')<br />
&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;line2&nbsp;in&nbsp;style:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;'color:&nbsp;white'&nbsp;in&nbsp;line2:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;randcolor=choice(colors)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.write(line2.replace('color:&nbsp;white','color:&nbsp;'+randcolor))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.write(line2)<br />
&nbsp;&nbsp;&nbsp;&nbsp;c.close()<br />
</code></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pythonism.wordpress.com/572/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pythonism.wordpress.com/572/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pythonism.wordpress.com/572/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pythonism.wordpress.com/572/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pythonism.wordpress.com/572/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pythonism.wordpress.com/572/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pythonism.wordpress.com/572/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pythonism.wordpress.com/572/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pythonism.wordpress.com/572/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pythonism.wordpress.com/572/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonism.wordpress.com&blog=3580594&post=572&subd=pythonism&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://pythonism.wordpress.com/2009/12/05/css-color-palette-randomiser/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a5395c548819c89c914016d32e44394a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pythonisms</media:title>
		</media:content>
	</item>
		<item>
		<title>New site made</title>
		<link>http://pythonism.wordpress.com/2009/10/22/new-site-made/</link>
		<comments>http://pythonism.wordpress.com/2009/10/22/new-site-made/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 14:03:00 +0000</pubDate>
		<dc:creator>pythonisms</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pythonism.wordpress.com/?p=567</guid>
		<description><![CDATA[I just made this site !!
http://www.paskbrothers.co.uk/
       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonism.wordpress.com&blog=3580594&post=567&subd=pythonism&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I just made this site !!</p>
<p><a href="http://www.paskbrothers.co.uk/">http://www.paskbrothers.co.uk/</a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pythonism.wordpress.com/567/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pythonism.wordpress.com/567/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pythonism.wordpress.com/567/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pythonism.wordpress.com/567/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pythonism.wordpress.com/567/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pythonism.wordpress.com/567/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pythonism.wordpress.com/567/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pythonism.wordpress.com/567/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pythonism.wordpress.com/567/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pythonism.wordpress.com/567/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonism.wordpress.com&blog=3580594&post=567&subd=pythonism&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://pythonism.wordpress.com/2009/10/22/new-site-made/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a5395c548819c89c914016d32e44394a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pythonisms</media:title>
		</media:content>
	</item>
		<item>
		<title>Strange ways to do multiplication</title>
		<link>http://pythonism.wordpress.com/2009/09/22/strange-ways-to-do-multiplication/</link>
		<comments>http://pythonism.wordpress.com/2009/09/22/strange-ways-to-do-multiplication/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 22:25:03 +0000</pubDate>
		<dc:creator>pythonisms</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[arithmetic]]></category>
		<category><![CDATA[bizarre]]></category>
		<category><![CDATA[multiplication]]></category>
		<category><![CDATA[strange]]></category>

		<guid isPermaLink="false">http://pythonism.wordpress.com/?p=553</guid>
		<description><![CDATA[Open a text editor and type a single letter. Now click in the whitespace and type ctrl+a to select the letter then ctrl+x to cut it.
Now take the first number you wish to multiply and holding down ctrl press v as many times as there are units in the first number.
Now press ctrl a again [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonism.wordpress.com&blog=3580594&post=553&subd=pythonism&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Open a text editor and type a single letter. Now click in the whitespace and type ctrl+a to select the letter then ctrl+x to cut it.</p>
<p>Now take the first number you wish to multiply and holding down ctrl press v as many times as there are units in the first number.</p>
<p>Now press ctrl a again to select all and then ctrl x to cut all the new letters out.</p>
<p>Now hold ctrl and press v as many times as there are units in the second number to be multiplied.</p>
<p>Now either read the number of bytes in the file from the status bar of the editor or save the file and read the size in bytes from your file manager. Strange, I know, but it does what it claims&#8230;</p>
<p>If anyone has any other strange ways to perform arithmetic operations then please share. We might stretch from dna based biological computing to using all the room light switches in your house to calculate in binary. Or might someone approximate integers by sugar lumps and divide hot tea in a measuring jug, then crystallise out the sugar and weigh it. It could be anything.</p>
<p>get thinking !</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pythonism.wordpress.com/553/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pythonism.wordpress.com/553/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pythonism.wordpress.com/553/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pythonism.wordpress.com/553/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pythonism.wordpress.com/553/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pythonism.wordpress.com/553/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pythonism.wordpress.com/553/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pythonism.wordpress.com/553/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pythonism.wordpress.com/553/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pythonism.wordpress.com/553/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonism.wordpress.com&blog=3580594&post=553&subd=pythonism&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://pythonism.wordpress.com/2009/09/22/strange-ways-to-do-multiplication/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a5395c548819c89c914016d32e44394a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pythonisms</media:title>
		</media:content>
	</item>
		<item>
		<title>Pressure and competitiveness in science</title>
		<link>http://pythonism.wordpress.com/2009/09/15/pressure-and-competitiveness-in-science/</link>
		<comments>http://pythonism.wordpress.com/2009/09/15/pressure-and-competitiveness-in-science/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 18:11:58 +0000</pubDate>
		<dc:creator>pythonisms</dc:creator>
				<category><![CDATA[Creative Writing]]></category>
		<category><![CDATA[Study Skills]]></category>
		<category><![CDATA[competitiveness]]></category>
		<category><![CDATA[hippies]]></category>
		<category><![CDATA[pressure]]></category>

		<guid isPermaLink="false">http://pythonism.wordpress.com/?p=550</guid>
		<description><![CDATA[A dear scientist friend of mine who shall remain nameless was once overwhelmed by grief and made a suicide attempt. I found him afterwards and insisted he go with me to the hospital. I will always remember his response &#8220;No I can&#8217;t&#8230; if any of my colleagues found out I had made a suicide attempt [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonism.wordpress.com&blog=3580594&post=550&subd=pythonism&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>A dear scientist friend of mine who shall remain nameless was once overwhelmed by grief and made a suicide attempt. I found him afterwards and insisted he go with me to the hospital. I will always remember his response &#8220;No I can&#8217;t&#8230; if any of my colleagues found out I had made a suicide attempt my career would be over.&#8221; Fortunately he was ok but it made me think&#8230;</p>
<p>Meritocracy usually assumes that excellence is best served by unbridled competition, but are there any scenarios where this inhibits human creativity ? i hold that there may be. Doing something for the love of it may provide a different kind of motivation to doing it to be the best. Sometimes the perfectionism of a craftsman who loves his work leads to better results than someone more based in their ego&#8217;s ambition to be the best. I feel this is also the difference between mathematics engaged in aesthetically and philosophically, and math where your aim is just to pass your exams.</p>
<p>Also isn&#8217;t it the case that competition helps the individual only when they win or succeed. In this respect it divides self esteem among people rather unfairly. I think we need to examine when and how our meritocratic principles work, and consider keeping control of ambition and striving. </p>
<p>As well as a skill to earn me a living I view math as an adjunct to meditation, and part of a spiritual drive to reach the truth. This has less to do with how good I am compared to others or how high I get ranked in my exams, although that may be a side effect. </p>
<p>Also taking a leisurely approach from time to time helps the knowledge become &#8220;my own&#8221;. I think this is critical in studying math since it is when you explore for yourself, armed with your unique questions that you see the real territory, more than slavishly following the path your lecturers always set. If math study is only imposed from outside there will always be a pressure to conform and achieve. Maths or other scientific tasks set by yourself for yourself are an enjoyable luxury and a comfort. By building positive associations in your mind like this you help your motivation. By paying attention to personal questions arising from a sense of mystery, you increase the dimensionality of your knowledge and its cohesion into one (or more) unified wholes. </p>
<p>If you enjoy a particular problem or area then that good feeling becomes associated with it in your mind and you can use it as fuel for more learning, and stay centred and at peace while you think.</p>
<p>Another thing about being too driven by competitiveness is that it may lead you to try to run before you can walk. There should be no shame about going back to basics, indeed this is essential. You have to know the basics like the back of your hand, but if someone asks you what you are doing and you say you are working on differential calculus in two variables they may think that is your only level. Humility is a good quality. Complexity is not the only measure of creativity too, many of the most important discoveries of science look so simple in hindsight. If you want to look oh-so-clever and only direct yourself towards complex difficult ideas you may miss simpler solutions that work. I believe we have to aim for a roundedness in this.</p>
<p>I liken my studies to mountaineering. Climbing the highest peak isn&#8217;t the only purpose. What about enjoying the view&#8230; ? or letting your experiences increase your wisdom and confidence in other areas. And just because yours is not a first ascent doesn&#8217;t necessarily mean you have had any less expertise or enjoyment than the pioneer.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pythonism.wordpress.com/550/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pythonism.wordpress.com/550/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pythonism.wordpress.com/550/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pythonism.wordpress.com/550/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pythonism.wordpress.com/550/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pythonism.wordpress.com/550/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pythonism.wordpress.com/550/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pythonism.wordpress.com/550/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pythonism.wordpress.com/550/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pythonism.wordpress.com/550/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonism.wordpress.com&blog=3580594&post=550&subd=pythonism&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://pythonism.wordpress.com/2009/09/15/pressure-and-competitiveness-in-science/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a5395c548819c89c914016d32e44394a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pythonisms</media:title>
		</media:content>
	</item>
		<item>
		<title>Montecarlo Mathemagic</title>
		<link>http://pythonism.wordpress.com/2009/09/13/montecarlo-mathemagic/</link>
		<comments>http://pythonism.wordpress.com/2009/09/13/montecarlo-mathemagic/#comments</comments>
		<pubDate>Sun, 13 Sep 2009 14:41:51 +0000</pubDate>
		<dc:creator>pythonisms</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[mathemagic]]></category>
		<category><![CDATA[area]]></category>
		<category><![CDATA[magic]]></category>
		<category><![CDATA[montecarlo]]></category>
		<category><![CDATA[pi]]></category>

		<guid isPermaLink="false">http://pythonism.wordpress.com/?p=547</guid>
		<description><![CDATA[Here&#8217;s a cool mathemagic trick.
If you inscribe a circle in a square as shown, then what is the ratio of the area of the circle to the area of the square ? Well lets take the case of a unit circle&#8230; its area must be pi * r^2 = pi. The area of the square [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonism.wordpress.com&blog=3580594&post=547&subd=pythonism&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Here&#8217;s a cool mathemagic trick.</p>
<p>If you inscribe a circle in a square as shown, then what is the ratio of the area of the circle to the area of the square ? Well lets take the case of a unit circle&#8230; its area must be pi * r^2 = pi. The area of the square will then be 2^2 = 4</p>
<p><img class="alignnone size-full wp-image-548" title="circsqu" src="http://pythonism.files.wordpress.com/2009/09/circsqu.jpg?w=213&#038;h=283" alt="circsqu" width="213" height="283" /></p>
<p>so the ratio must be pi/4</p>
<p>We are going to use this fact to obtain an estimate of pi.</p>
<p>Imagine drawing the figure on the ground with chalk. Now scatter one or more handfuls of rice or stones over it until you have a reasonably equal distribution. The rice can fall anywhere around and inside the shapes, it doesn&#8217;t matter if the grains spill over.</p>
<p>Now, if the density of the grains inside the circle and the square are reasonably constant then  the density per unit area of the grains should be constant, this means that the ratio of the number of grains inside the circle to the number inside the square should be the ratio of the two areas, so count the numbers, divide and multiply by 4 and you have an estimate of pi !</p>
<p>This was done by using a randomising method, and thus falls loosely under the category of what are called Montecarlo Simulations. They are used in many areas from finance to weather prediction.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pythonism.wordpress.com/547/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pythonism.wordpress.com/547/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pythonism.wordpress.com/547/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pythonism.wordpress.com/547/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pythonism.wordpress.com/547/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pythonism.wordpress.com/547/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pythonism.wordpress.com/547/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pythonism.wordpress.com/547/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pythonism.wordpress.com/547/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pythonism.wordpress.com/547/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonism.wordpress.com&blog=3580594&post=547&subd=pythonism&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://pythonism.wordpress.com/2009/09/13/montecarlo-mathemagic/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a5395c548819c89c914016d32e44394a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pythonisms</media:title>
		</media:content>

		<media:content url="http://pythonism.files.wordpress.com/2009/09/circsqu.jpg" medium="image">
			<media:title type="html">circsqu</media:title>
		</media:content>
	</item>
		<item>
		<title>Bayes&#8217; Theorem and how to Dismiss Paranoia&#8230;</title>
		<link>http://pythonism.wordpress.com/2009/08/30/bayes-theorem-and-how-to-dismiss-paranoia/</link>
		<comments>http://pythonism.wordpress.com/2009/08/30/bayes-theorem-and-how-to-dismiss-paranoia/#comments</comments>
		<pubDate>Sun, 30 Aug 2009 22:24:32 +0000</pubDate>
		<dc:creator>pythonisms</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[probability]]></category>
		<category><![CDATA[Bayes]]></category>
		<category><![CDATA[bayesian]]></category>
		<category><![CDATA[conditional probability]]></category>
		<category><![CDATA[evidence]]></category>

		<guid isPermaLink="false">http://pythonism.wordpress.com/?p=542</guid>
		<description><![CDATA[It is amazing how a very simple mathematical formula can hold such a world of detail and be such a powerful tool in understanding reality.
One such formula is Bayes&#8217; Theorem
This theorem tells us about the conditional probability of one event given that another event has been observed, what that is really about is the following:
How [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonism.wordpress.com&blog=3580594&post=542&subd=pythonism&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>It is amazing how a very simple mathematical formula can hold such a world of detail and be such a powerful tool in understanding reality.</p>
<p>One such formula is <strong>Bayes&#8217; Theorem</strong></p>
<p>This theorem tells us about the conditional probability of one event given that another event has been observed, what that is really about is the following:</p>
<p>How do we assess the likelihood of an event given a piece of evidence that may or may not have bearing on it? </p>
<p>The idea can be built up quite simply. let&#8217;s think of two events called A and B. The probabilities of each of these happening are represented as P(A) and P(B).</p>
<p>We&#8217;ll immediately make this concrete to explain it best. Let&#8217;s denote as follows</p>
<p><strong>A</strong> : John is guilty of the murder<br />
<strong>B</strong> : John was found at the scene after the dead body was found with blood on his hands</p>
<p>A has yet to be established, but we know B has already happened.</p>
<p>Well Bayes&#8217;s Theorem gives us one possible way to do some calculation about this. Time for notation&#8230;</p>
<p><strong>P(A|B)</strong> is how we write &#8220;The probability of A happening given that B has happened.</p>
<p>it turns out that P(A|B) can be expressed as</p>
<p><strong>P(A|B) = P(A) * ( P(B|A) / P(B) )</strong></p>
<p>So in this case P(A) will be the probability of a freely chosen individual in the sample being guilty of a murder. This is the basic rate when we have absolutely no evidence to narrow it down. In other words P(A) will be the murder rate in whatever country we are in.</p>
<p>P(B) will be the likelihood of a random individual being found with blood on their hands at a crime scene</p>
<p>and P(B|A) is the probability of someone being found with blood on their hands when it is given and therefore certain they have just committed a murder. In other words this is a quantity that is the correlation or degree of connection between murder and having blood on your hands.</p>
<p>To look at our example again, maybe if someone worked at an abbatoir or butcher&#8217;s they might frequently be found with blood on their hands, so it is not self evident that the given evidence guarantees the probability P(A) must be 1. However it does seem likely that this kind of evidence is compelling, therefore we expect the correlation of bloody hands with murders to be quite high. P(B) is the denominator in the right hand term though, so if B is rare  and therefore P(B) small then that enhances our evidence&#8217;s power to convict. P(A) is the starting likelihood of john being a murderer before we found the evidence. So you can see that</p>
<p><strong>P(B|A) / P(B)</strong> is the amount by a factor of which our evidence alters the likelihood of A. If P(B|A) is small then our evidence may not help us that much, unless P(B) is even smaller. If P(B|A) is big, i.e. if the two events of bloody hands and guilt are strongly correlated then the evidence may be helpful. If there&#8217;s slight correlation but B is a common event then once again the evidence may not be much help. You can play around with the figures yourself but its such a beautifully simple formula that it becomes quite easy to visualise.</p>
<p>One of the noted applications of Bayesian Reasoning of this kind is in using healthcare data about the error rates of clinical tests. A scientist online quoted an example of the Mammogram as a test for breast cancer, and to what extent we can expect a positive mammogram test to lead us to believe that the subject has breast cancer. Interestingly the figures work out to show that a small but appreciable false positive rate and a similarly small nonzero false negative rate seriously diminish the validity of the test. Here&#8217;s how it goes:</p>
<p>1% of women at age forty who participate in routine screening have breast cancer.  80% of women with breast cancer will get positive mammographies.  9.6% of women without breast cancer will also get positive mammographies.  A woman in this age group had a positive mammography in a routine screening.  What is the probability that she actually has breast cancer?</p>
<p>quoted from http://yudkowsky.net/rational/bayes</p>
<p>well if A is having cancer and B is testing positive then <strong>P(B|A)</strong> should be the frequency of people who test positive while definitely and verifiably having cancer.</p>
<p>so <strong>P(A),</strong> the <em>incidence of cancer in the group</em>, is clearly 0.01</p>
<p><strong>P(B)</strong> is a little subtler, since it has to include the false positives rate, I make</p>
<p><strong>P(B) = (0.01 * 0.8) + (0.096 * (1- 0.01)) = 0.10304</strong></p>
<p>this is the <em>incidence of positive tests</em>, real illness notwithstanding</p>
<p>and finally</p>
<p><strong>P(B|A) = 0.8</strong></p>
<p>which is 1 minus the <em>false negative rate</em> (number of people with cancer who don&#8217;t test positive)</p>
<p>so here:</p>
<p><strong>P(A|B)</strong> = 0.01 * (0.8 / 0.10304) = <strong>0.07763 = 7.763%</strong></p>
<p>In other words the test is really not a conclusively strong piece of evidence. The catch is that there are so many more women who don&#8217;t have breast cancer that even a relatively small proportion of false positives (people who the test says have cancer but who don&#8217;t in reality) changes the figures quite drastically.</p>
<p>So there we have it ! The great thing about this stuff is that there is such a myriad profusion of examples we can think up. Any area where some basic stats are known can really elucidate and sharpen our understanding of how evidence based reasoning works out in practice. </p>
<p>I have also personally found that going to the trouble of some rule of thumb calculations can even help allay personal fears about  nasty and dangerous events, which duly do prove to be rather unlikely. <em>Maths beats paranoia every time !!</em></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pythonism.wordpress.com/542/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pythonism.wordpress.com/542/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pythonism.wordpress.com/542/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pythonism.wordpress.com/542/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pythonism.wordpress.com/542/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pythonism.wordpress.com/542/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pythonism.wordpress.com/542/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pythonism.wordpress.com/542/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pythonism.wordpress.com/542/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pythonism.wordpress.com/542/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonism.wordpress.com&blog=3580594&post=542&subd=pythonism&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://pythonism.wordpress.com/2009/08/30/bayes-theorem-and-how-to-dismiss-paranoia/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a5395c548819c89c914016d32e44394a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pythonisms</media:title>
		</media:content>
	</item>
		<item>
		<title>Of Magic Squares and Combinatorial Explosions</title>
		<link>http://pythonism.wordpress.com/2009/08/27/of-magic-squares-and-combinatorial-explosions/</link>
		<comments>http://pythonism.wordpress.com/2009/08/27/of-magic-squares-and-combinatorial-explosions/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 19:10:02 +0000</pubDate>
		<dc:creator>pythonisms</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[brute force]]></category>
		<category><![CDATA[combinatorial explosion]]></category>
		<category><![CDATA[magic squares]]></category>

		<guid isPermaLink="false">http://pythonism.wordpress.com/?p=518</guid>
		<description><![CDATA[A magic square is an n x n grid of numbers where each row, column and diagonal sum to the same amount. Usually the first n^2 integers are used.
There&#8217;s a famous painting by Durer containing a 4 x 4 magic square.

Shall we try bravely to write some code to generate these objects ?
Let&#8217;s start with [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonism.wordpress.com&blog=3580594&post=518&subd=pythonism&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>A magic square is an n x n grid of numbers where each row, column and diagonal sum to the same amount. Usually the first n^2 integers are used.</p>
<p>There&#8217;s a famous painting by Durer containing a 4 x 4 magic square.<br />
<img class="alignnone size-medium wp-image-521" title="durer" src="http://pythonism.files.wordpress.com/2009/08/durer.jpg?w=243&#038;h=300" alt="durer" width="243" height="300" /></p>
<p>Shall we try bravely to write some code to generate these objects ?</p>
<p>Let&#8217;s start with a 3 x 3. There are nine integers, so the most apparent brute force method would be to try every permutation of these nine counting sequentially from top left to bottom right, and check with a conditional statement whether the rows and columns etc add up. It sounds pretty straightforward&#8230; How do we get every permutation ? well there&#8217;s a built in module in the python library called itertools, which includes a function called permutations that does exactly that., so we&#8217;ll start with</p>
<p><code>from itertools import permutations</code></p>
<p>This can take a list as its input, to get every one for 1-9 we could say:</p>
<p><code>a=[1,2,3,4,5,6,7,8,9]<br />
for x in permutations(a,9):<br />
&nbsp;&nbsp;&nbsp;&nbsp;print(x)</code></p>
<p>which starts:</p>
<p><code><br />
(1,2,3,4,5,6,7,8,9)<br />
(1,2,3,4,5,6,7,9,8)<br />
</code></p>
<p>etc</p>
<p>each row and column must add up to (1+2+3+4+5+6+7+8+9)/3 = 15</p>
<p>then for each possible permutation we need to check whether the rows add up to 15, this can be done like this<br />
<code>from itertools import permutations<br />
x=[1,2,3,4,5,6,7,8,9]<br />
for&nbsp;a&nbsp;in&nbsp;permutations(x,9):<br />
&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;a[0]+a[1]+a[2]==15&nbsp;and&nbsp;a[3]+a[4]+a[5]==15:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;a[6]+a[7]+a[8]==15&nbsp;and&nbsp;a[0]+a[3]+a[6]==15:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;a[1]+a[4]+a[7]==15&nbsp;and&nbsp;a[2]+a[5]+a[8]==15:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;a[0]+a[4]+a[8]==15&nbsp;and&nbsp;a[2]+a[4]+a[6]==15:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(a[0:3])<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(a[3:6])<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(a[6:])<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print()</code><br />
You could of course put the conditional all on one line, but i staggered it so it looks better on the page width for this blog. When run we get:</p>
<p><code>&gt;&gt;&gt;<br />
(2, 7, 6)<br />
(9, 5, 1)<br />
(4, 3, 8 )</code></p>
<p><code>(2, 9, 4)<br />
(7, 5, 3)<br />
(6, 1, 8 )</code></p>
<p><code>(4, 3, 8 )<br />
(9, 5, 1)<br />
(2, 7, 6)</code></p>
<p><code>(4, 9, 2)<br />
(3, 5, 7)<br />
(8, 1, 6)</code></p>
<p><code>(6, 1, 8 )<br />
(7, 5, 3)<br />
(2, 9, 4)</code></p>
<p><code>(6, 7, 2)<br />
(1, 5, 9)<br />
(8, 3, 4)</code></p>
<p><code>(8, 1, 6)<br />
(3, 5, 7)<br />
(4, 9, 2)</code></p>
<p><code>(8, 3, 4)<br />
(1, 5, 9)<br />
(6, 7, 2)</code><br />
<code>&gt;&gt;&gt; </code></p>
<p>(This counts reflections and transpositions as different squares by the way.)</p>
<p>Ok so that is fairly straightforward, and the code runs quite fast too. You might think that we can easily scale up to find some bigger ones using this method&#8230; what about trying one to find all possible 4&#215;4 squares ?</p>
<p>the magic sum must be (1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16)/4 = 34</p>
<p>the code would be:</p>
<p><code><br />
from itertools import permutations<br />
x=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]<br />
for&nbsp;a&nbsp;in&nbsp;permutations(x,16):<br />
&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;a[0]+a[1]+a[2]+a[3]==34&nbsp;and&nbsp;a[4]+a[5]+a[6]+a[7]==34:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;a[8]+a[9]+a[10]+a[11]==34&nbsp;and&nbsp;a[12]+a[13]+a[14]+a[15]==34:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;a[0]+a[4]+a[8]+a[12]==34&nbsp;and&nbsp;a[1]+a[5]+a[9]+a[13]==34:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;a[2]+a[6]+a[10]+a[14]==34&nbsp;and&nbsp;a[3]+a[7]+a[11]+a[15]==34:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;a[0]+a[5]+a[10]+a[15]==34&nbsp;and&nbsp;a[3]+a[6]+a[9]+a[12]==34:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(a[0:4])<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(a[4:8])<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(a[8:12])<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(a[12:])<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print()<br />
</code></p>
<p>but when you start it running nothing happens for a long time. Ok maybe this brute force method would be too slow. How many permutations is it going to try ? well there are 16 digits in the list, so in fact there will be 16!, 16 factorial lists in the set.</p>
<p>A quick bit of code may show us how big this number is:</p>
<p><code>def&nbsp;fac(x):<br />
&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;x==1:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;1<br />
&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;x*fac(x-1)<br />
print&nbsp;(fac(16))</code></p>
<p>it is 20922789888000 or about 21 trillion. So the conditionals in the 4&#215;4 code will be executed that many times. how long does each execution of the conditionals take ? Let&#8217;s time it. this code just puts the permutations in a list, which would be quicker,  but we can still use this to get an approximate lower bound for runtime</p>
<p><code>from time import time<br />
from itertools import permutations<br />
for&nbsp;a&nbsp;in&nbsp;range(2,15):<br />
&nbsp;&nbsp;&nbsp;&nbsp;ti=time()<br />
&nbsp;&nbsp;&nbsp;&nbsp;c=[]<br />
&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;x&nbsp;in&nbsp;permutations(range(a),a):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.append(x)<br />
&nbsp;&nbsp;&nbsp;&nbsp;print(a,len(c),time()-ti)<br />
    print ()<br />
</code></p>
<p>well to process all the 3628800 permutations of digits 1-10 takes about 35 seconds on my machine. so using very approximate estimates it should take about 16&#215;15x14&#215;13x12&#215;11x35 seconds. this is 201801600 seconds or roughly 6 years.</p>
<p>Ok so maybe we have to rethink this&#8230;</p>
<p>Well that&#8217;s all I&#8217;ll say for now, but it serves as a useful demonstration of the power of what are called combinatorial explosions. With combinations or permutations of even relatively small sets the numbers rapidly get real big. If anyone can work out a clever way to generate all the 4&#215;4 squares then please post.</p>
<p>Regarding these explosions a vivid and beguiling image I have is of having to consider all the ways that each of the ~10^80 particles in the universe could be listed in order. This is a large number. That is an understatement.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pythonism.wordpress.com/518/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pythonism.wordpress.com/518/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pythonism.wordpress.com/518/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pythonism.wordpress.com/518/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pythonism.wordpress.com/518/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pythonism.wordpress.com/518/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pythonism.wordpress.com/518/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pythonism.wordpress.com/518/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pythonism.wordpress.com/518/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pythonism.wordpress.com/518/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonism.wordpress.com&blog=3580594&post=518&subd=pythonism&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://pythonism.wordpress.com/2009/08/27/of-magic-squares-and-combinatorial-explosions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a5395c548819c89c914016d32e44394a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pythonisms</media:title>
		</media:content>

		<media:content url="http://pythonism.files.wordpress.com/2009/08/durer.jpg?w=243" medium="image">
			<media:title type="html">durer</media:title>
		</media:content>
	</item>
	</channel>
</rss>