<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.2.1" -->
<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/"
	>

<channel>
	<title>Tafakuri</title>
	<link>http://tafakuri.net</link>
	<description>fikira nzito kuhusu jambo fulani; mazingatio; taamuli</description>
	<pubDate>Tue, 10 May 2011 00:50:49 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.1</generator>
	<language>en</language>
			<item>
		<title>Kenya&#8217;s Chief Justice selection</title>
		<link>http://tafakuri.net/?p=79</link>
		<comments>http://tafakuri.net/?p=79#comments</comments>
		<pubDate>Mon, 09 May 2011 15:57:36 +0000</pubDate>
		<dc:creator>Mucheru</dc:creator>
		
		<category><![CDATA[siasa]]></category>

		<guid isPermaLink="false">http://tafakuri.net/?p=79</guid>
		<description><![CDATA[ Kenya is going through the process of selecting the next chief justice. Candidates for the post are being interviewed by a panel of prominent lawyers in a public hearing. This is a new selection process - previously the chief justice was appointed by the president and did not need to be accountable to anyone [...]]]></description>
			<content:encoded><![CDATA[<p> Kenya is going through the process of selecting the next chief justice. Candidates for the post are being interviewed by a panel of prominent lawyers in a public hearing. This is a new selection process - previously the chief justice was appointed by the president and did not need to be accountable to anyone else. You can tell that the lawyers are enjoying digging into the (often ill-prepared) judges. Ahmednasir, the former law society of Kenya chairman had this question for Lady Justice Ang&#8217;awa:</p>
<blockquote><p>Your judgments consist of one line or one paragraph rulings, skeleton in nature and lacks depth. It is evident you have a problem writing in prose. Do you think writing in poetry will help or capture the essence of justice?</p></blockquote>
<p>Reading the coverage of the hearings is quite entertaining and also illuminating. The interviewers are bringing up particular cases that the judges handled and putting them to task for decisions handed suspiciously in favor of connected politicians or businessmen. The best part of this is that the judges never thought they would have to explain their decisions and some are still indignant when asked to.</p>
<p>Going back to Ang&#8217;awa - how does a judge of the High Court make a career of issuing one-line rulings? Sure, it looks good for her statistics that she clears many cases. At this level, though, the goal of a judge is not simply to adjudicate conflicts - they interpret the law, provide guidance to lower courts<span>  </span>and establish precedent which will guide future courts. How is one to infer a legal doctrine from one-line rulings? My advice for the justice is to branch out. Try some verses as Ahmednasir suggested. Maybe combine the love for brevity with poetic skills and write some haiku.</p>
]]></content:encoded>
			<wfw:commentRss>http://tafakuri.net/?feed=rss2&amp;p=79</wfw:commentRss>
		</item>
		<item>
		<title>Two numbers</title>
		<link>http://tafakuri.net/?p=78</link>
		<comments>http://tafakuri.net/?p=78#comments</comments>
		<pubDate>Mon, 30 Aug 2010 13:57:25 +0000</pubDate>
		<dc:creator>Mucheru</dc:creator>
		
		<category><![CDATA[hisabati]]></category>

		<guid isPermaLink="false">http://tafakuri.net/?p=78</guid>
		<description><![CDATA[Here is an interesting puzzle that was posted to one of the discussion groups at work.

A teacher tells two students that he is thinking of two natural numbers greater than 1.  He tells the first student the product of the two numbers and the second one their sum.  The students then have the following dialog:
&#160;
     [...]]]></description>
			<content:encoded><![CDATA[<p style="margin: 0in; font-family: Calibri; font-size: 11pt">Here is an interesting puzzle that was posted to one of the discussion groups at work.</p>
<blockquote>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">A teacher tells two students that he is thinking of two natural numbers greater than 1.  He tells the first student the product of the two numbers and the second one their sum.  The students then have the following dialog:</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">     <span style="color: red; font-weight: bold">First</span>: I do not know their sum.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">     <span style="color: #0070c0; font-weight: bold">Second</span>: I knew that.  Their sum is less than 14.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">     <span style="color: red; font-weight: bold">First</span>: I knew that, and now I know the numbers.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">     <span style="color: #0070c0; font-weight: bold">Second</span>: So do I.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">What were the numbers and how did they figure them out?</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</blockquote>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt"><span style="font-weight: bold">Solution</span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">Let’s say that the two numbers that the teacher is thinking of are<span style="font-style: italic"> a</span> and <span style="font-style: italic">b</span>. Lets say that the product <span style="font-style: italic">a*b = n</span> and the sum <span style="font-style: italic">a+b = m</span>.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">When the first student gets the product <span style="font-style: italic">n</span>, it is fair to assume that he tries to factor <span style="font-style: italic">n</span> to find out the two numbers. There may be multiple ways of factoring <span style="font-style: italic">n</span> into two integers greater than 1 so we can assume he finds all such pairs.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">When the second student gets the sum <span style="font-style: italic">m</span>, he tries to find all integer pairs <span style="font-style: italic">{i,j}</span> such that <span style="font-style: italic">i+j = m</span>.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">Both students have their lists of candidate pairs before they start their conversation.  We can now step through their statements.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt"><span style="font-weight: bold">First student says: &#8221; I do not know their sum&#8221;</span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">Remember that the first student has the product <span style="font-style: italic">n</span> and has determined all pairs <span style="font-style: italic">{i,j}</span> such that <span style="font-style: italic">i*j=n</span>. If he cannot find the sum, this implies that there are multiple ways of factoring <span style="font-style: italic">n</span> into two integers. Otherwise, if there was only one way of factoring <span style="font-style: italic">n</span> into two, he would have a single pair <span style="font-style: italic">{i,j}</span> and would be able to determine the sum.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">If <span style="font-style: italic">n</span> cannot be factored  uniquely into two numbers, then the target numbers <span style="font-style: italic">a</span> and <span style="font-style: italic">b</span> cannot both be prime (maybe one or zero of them is prime). Otherwise, if <span style="font-style: italic">a</span> and <span style="font-style: italic">b</span> were both prime, then there would only be one way of factoring <span style="font-style: italic">n</span> into two.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt"><span style="font-weight: bold"></span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt"><span style="font-weight: bold">Second student says: &#8220;I knew that the first student could not know the sum&#8221;</span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">Recall that second student has the sum <span style="font-style: italic">m</span> and has enumerated all pairs <span style="font-style: italic">{i,j}</span> such that <span style="font-style: italic">i+j = m</span>. Since he has these pairs, he can calculate all the possible products of <span style="font-style: italic">i*j</span> and this will help him know what kind of info the first student has.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">When the first student says that he cannot uniquely determine the sum, it confirms what second knew. In second student&#8217;s list of pairs <span style="font-style: italic">{i,j}</span> , there is no pair such that, given the product, you can uniquely identify the sum.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">The only way that second student could have known for sure that the first student could not determine the sum is if on the list of pairs that second student has, there is no pair <span style="font-style: italic">{i,j}</span> such that both <span style="font-style: italic">i</span> and <span style="font-style: italic">j</span> are prime.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">In other words,  <span style="font-style: italic">m</span> is an integer that cannot be created by adding two primes.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt"><span style="font-weight: bold">Second student says: The sum is less than 14</span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">This tells us that <span style="font-style: italic">1 &lt; m &lt; 14</span>. From the previous section , we also know that <span style="font-style: italic">m</span> cannot be created by adding two primes. This tells us that m = 11.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">The derivation is as follows:</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt"><span style="font-style: italic">m</span> is in the set {2,3,4,5,6,7,8,9,10,11,12,13}</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">2 is out because the only <span style="font-style: italic">{i,j}</span> such that <span style="font-style: italic">i+j = </span>2 is <span style="font-style: italic">{</span>1,1<span style="font-style: italic">}</span> and we know that both candidate numbers are greater than 1.</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">3 is out because the only <span style="font-style: italic">{i,j}</span> s.t. <span style="font-style: italic">i+j=3</span> is {1,2}</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">4 is out because it can be created by 2+2 where 2 is prime</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">5 is out. It can be created by 2+3</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">6 is out. It can be created by 3+3</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">7 is out. It can be created by 2+5</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">8 is out. It can be created by 3+5</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">9 is out. It can be created by 2+7</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">10 is out. It can be created by 3+7</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">12 is out. It can be created by 5+7</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">13 is out. It can be created by 2+11</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt"><span style="font-weight: bold">First student says: &#8220;I knew that the sum m &lt; 14&#8243;</span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">Remember that the first student has the product <span style="font-style: italic">n</span> and has determined all pairs <span style="font-style: italic">{i,j}</span> such that <span style="font-style: italic">i*j=n</span>.  Since he has the pairs, he has also summed them and he knows for sure that for all <span style="font-style: italic">{i,j}, i+j &lt;14</span>.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">In other words, <span style="font-style: italic">n</span> is an integer that can be factored into two in multiple ways but however you do it, the two numbers you end up with always add up to less than 14.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">This is the final piece. The final derivation is as follows:</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">We know that  <span style="font-style: italic">i</span> and <span style="font-style: italic">j</span> are both numbers in <span style="font-style: italic">{2,3,4,5,6,7,8,9,10,11,12,13}</span> (i.e. neither <span style="font-style: italic">i</span> nor <span style="font-style: italic">j</span> can be greater than 14)</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">We can enumerate all pairs <span style="font-style: italic">{i,j}</span> such that <span style="font-style: italic">i+j &lt; 14</span></p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">{2|2,3,4,5,6,7,8,9,10,11}</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">{3|3,4,5,6,7,8,9,10}</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">{4|4,5,6,7,8,9}</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">{5|5,6,7,8}</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">{6|6,7}</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">Where the notation {x|y,z} is shorthand for the pairs &#8220;the pairs where the smaller number is x and the larger is either y or z&#8221; (i.e. {x,y} and {x,z})</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">We can then eliminate all pairs where <span style="font-style: italic">i</span> and <span style="font-style: italic">j</span> are both prime. This leaves.</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">{2|4,6,8,9,10}</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">{3|4,6,8,9,10}</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">{4|4,5,6,7,8,9}</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">{5|6,8}</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">{6|6,7}</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">Consider the products of pairs <span style="font-style: italic">i*j</span></p>
<table valign="top" cellPadding="0" cellSpacing="0" border="1" style="border-collapse: collapse; direction: ltr; border: #a3a3a3 1pt solid">
<tr>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">4</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">5</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">6</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">7</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">8</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">9</p>
</td>
<td style="width: 0.716in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">10</p>
</td>
</tr>
<tr>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">2</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">8</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">12</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">16</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">18</p>
</td>
<td style="width: 0.716in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">20</p>
</td>
</tr>
<tr>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">3</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">12</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">18</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">24</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">27</p>
</td>
<td style="width: 0.716in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">30</p>
</td>
</tr>
<tr>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">4</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">16</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">20</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">24</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">28</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">32</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">36</p>
</td>
<td style="width: 0.716in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
</tr>
<tr>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">5</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">30</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">40</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
<td style="width: 0.716in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
</tr>
<tr>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">6</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">36</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">42</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
<td style="width: 0.716in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
</tr>
</table>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">Eliminate all products that can be created by multiplying numbers that add up to &gt;=14. This eliminates 24, 28, 30, 32, 36, 40 and 42 because :</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">36 = 18*2, and 18+2&gt;14</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">30 = 15*2, and 15+2 &gt; 14</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">24 = 12*2, and 12+2 = 14</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">28 = 14*2</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">42=21*2</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">32=16*2</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">40=20*2</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">This gives us the grid:</p>
<table valign="top" cellPadding="0" cellSpacing="0" border="1" style="border-collapse: collapse; direction: ltr; border: #a3a3a3 1pt solid">
<tr>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">4</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">5</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">6</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">8</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">9</p>
</td>
<td style="width: 0.716in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">10</p>
</td>
</tr>
<tr>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">2</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">8</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">12</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">16</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">18</p>
</td>
<td style="width: 0.716in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">20</p>
</td>
</tr>
<tr>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">3</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">12</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">18</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">27</p>
</td>
<td style="width: 0.716in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
</tr>
<tr>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">4</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">16</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">20</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
<td style="width: 0.667in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
<td style="width: 0.716in; vertical-align: top; border: #a3a3a3 1pt solid; padding: 4pt">
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
</td>
</tr>
</table>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">Which gives the pairs</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">{2|4,6,8,9,10}</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">{3|4,6,9}</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt">{4|4,5}</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt">We already know that <span style="font-style: italic">i+j = 11</span>. Looking at our pairs, we determine that the answer is {2,9}</p>
]]></content:encoded>
			<wfw:commentRss>http://tafakuri.net/?feed=rss2&amp;p=78</wfw:commentRss>
		</item>
		<item>
		<title>Project Euler: Happy numbers</title>
		<link>http://tafakuri.net/?p=77</link>
		<comments>http://tafakuri.net/?p=77#comments</comments>
		<pubDate>Sun, 20 Sep 2009 07:54:45 +0000</pubDate>
		<dc:creator>Mucheru</dc:creator>
		
		<category><![CDATA[project euler]]></category>

		<category><![CDATA[hisabati]]></category>

		<guid isPermaLink="false">http://tafakuri.net/?p=77</guid>
		<description><![CDATA[Problem 92 from Project Euler asks us to find the number of non-Happy numbers under 10 million:
A number chain is created by continuously adding the square of the digits in a number to form a new number until it has been seen before.
For example,
44 -&#62; 32 -&#62; 13 -&#62; 10 -&#62; 1 -&#62; 1
85 -&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>Problem 92 from Project Euler asks us to find the number of non-Happy numbers under 10 million:</p>
<blockquote><p>A number chain is created by continuously adding the square of the digits in a number to form a new number until it has been seen before.</p>
<p>For example,</p>
<p>44 -&gt; 32 -&gt; 13 -&gt; 10 -&gt; <strong>1</strong> -&gt; <strong>1</strong><br />
85 -&gt; <strong>89</strong> -&gt; 145 -&gt; 42 -&gt; 20 -&gt; 4 -&gt; 16 -&gt; 37 -&gt; 58 -&gt; <strong>89</strong></p>
<p>Therefore any chain that arrives at 1 or 89 will become stuck in an endless loop. What is most amazing is that EVERY starting number will eventually arrive at 1 or 89.</p>
<p>How many starting numbers below ten million will arrive at 89?</p></blockquote>
<p>The numbers that have a sequence ending in 1 are called the ‘Happy’ numbers (making the rest non-Happy).</p>
<p>As with most Project Euler problems, this could be solved by brute force. Following the definition of the sequences, we can run through all numbers from 1 to 10 million. For each number, split it into its constituent digits, square each digit and sum them all. If the sum of squares of digits is 89, we’ve found a match. Otherwise, test the new number for a match (split digits, square etc). The problem with this approach is that you will have to evaluate at lest 10 million numbers. In a naive implementation, you would actually end up looking at some numbers multiple times. For example, in the series starting at 85 above, you would end up looking at 145 and you may look at it again when you consider the series starting at 145. Clearly, this approach does not scale.</p>
<p>The first optimization we can make is ensure we evaluate a number only once. To do this, we can store a lookup table with all the numbers that have a series ending in 89. If we evaluate a series that ends in 89, we know that any numbers in the series ends in 89 (i.e. 85,145,20 etc from above).</p>
<p>The second optimization  comes when we notice that the largest possible sum of the squares of digits in a number for our test is (9<sup>2</sup>)x7 = 567 (from the number 9999999). This reduces the number of sequence evaluations we have to make.  We can construct our lookup table showing numbers between 0 and 567 that end in 89. Then for each number in our test range from 0 to 10 million, we simply calculate the sum of squares of digits and check our lookup table to see if the value would terminate in 89.</p>
<p>The third optimization is based on the realization that the sum of squares of digits function does not change if you reorder the digits. That is, the sum of squares for 123 is the same as for 213, 312, 321 etc. So if the sequence starting at 123 ends at 89, we know that 312, 213 etc will end at 89 as well. Formally, this means that the sum-of-squares-of-digits function partitions the set of integers into <a href="http://en.wikipedia.org/wiki/Equivalence_class">Equivalence classes</a> such that a~b if a and b contain the same digits. Using this optimization,we only need to check sequences for 11,440 numbers – almost 1000 times smaller than the 10 million checks that the brute force approach would have us make. However, to use this approach, we need to determine the size of each equivalence class. That way, if we determine that that a particular number ends in 89, then we can update our count of matching numbers by adding the full size of the numbers equivalence class.</p>
<p>We turn to combinatorics to determine the size of each class. For any one number, its equivalence class is constructed by creating permutations of the digits. The size of the class is therefore given by the number of unique permutations of digits in a sequence. We need to account for repetitions of indistinguishable objects: that is, given the sequence 100335, we need to account for that the the 3s and 0s can be interchanged in permutations without resulting in different integers. The formula and reasoning for such a count is explained at <a href="http://www.andrews.edu/~calkins/math/webtexts/prod02.htm" title="http://www.andrews.edu/~calkins/math/webtexts/prod02.htm">http://www.andrews.edu/~calkins/math/webtexts/prod02.htm</a> under the title “Permutations with repeated elements”. The eventual formula is;</p>
<p><sub>n</sub>P<sub>r1</sub> <sub>r2</sub>&#8230;<sub>rk</sub> = n! /( r<sub>1</sub>!r<sub>2</sub>!&#8230;r<sub>k</sub>!)</p>
<p>where we are arranging n elements where the first element occurs r1 times, the second r2 times …</p>
<p>We still need to figure out how to make sure we pick one (and only one) element from each equivalence class for our sequence calculations. Each class is composed of numbers that contain the same digits in some order. We can represent each class by creating a string with the non-decreasing sequence of digits that define the class. Generating all strings of of length 7 with non-decreasing sequence of digits will give us exactly one representative from each equivalence class.</p>
<p>With these optimizations, we solve the problem in 0.5 seconds using python. We can also count all the non-Happy numbers under 10^100 in under a minute. We can further reduce our computations  by noticing that there are much fewer happy numbers than unhappy numbers – there are 20 happy numbers under 100. Since all numbers are either Happy or non-Happy, we can determine the number of non-Happy numbers by counting happy numbers and subtracting from the count of numbers in range.</p>
]]></content:encoded>
			<wfw:commentRss>http://tafakuri.net/?feed=rss2&amp;p=77</wfw:commentRss>
		</item>
		<item>
		<title>Nerdy t-shirts</title>
		<link>http://tafakuri.net/?p=76</link>
		<comments>http://tafakuri.net/?p=76#comments</comments>
		<pubDate>Sun, 06 Sep 2009 04:29:07 +0000</pubDate>
		<dc:creator>Mucheru</dc:creator>
		
		<category><![CDATA[tarakirishi]]></category>

		<category><![CDATA[sayansi]]></category>

		<guid isPermaLink="false">http://tafakuri.net/?p=76</guid>
		<description><![CDATA[Working at a software company, I&#8217;ve gotten to see a fair share of nerdy t-shirts. There are the those celebrating various video games, software launches etc. Then there are the programming references (&#8221;Wanna grab a byte?&#8221;).
Probably nobody does programming humor better than XKCD - their &#8220;my code is compiling&#8221; and &#8220;sudo make me a sandwich&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>Working at a software company, I&#8217;ve gotten to see a fair share of nerdy t-shirts. There are the those celebrating various video games, software launches etc. Then there are the programming references (&#8221;Wanna grab a byte?&#8221;).</p>
<p>Probably nobody does programming humor better than <a href="http://xkcd.com">XKCD</a> - their &#8220;<a href="http://xkcd.com/303/">my code is compiling</a>&#8221; and &#8220;<a href="http://xkcd.com/149/">sudo make me a sandwich</a>&#8221; shirts are pretty common. This week, I saw two shirts that are in contention for the title of &#8220;Most Awesome T-shirt Ever&#8221;.</p>
<p>The first had a decidedly complicated looking equation on the front, giving the value of V<sub>s</sub>. The caption read &#8220;<a href="http://www.youtube.com/watch?v=pWS8Mg-JWSg&amp;feature=related">Airspeed velocity of an unladen swallow</a>&#8220;. Priceless. I was tempted to ask if the swallow was African or European.</p>
<p>The second had a big bold &#8220;<a href="http://en.wikipedia.org/wiki/Imaginary_number">i<sup>2</sup></a>&#8221; on the front with the caption &#8220;keeping It <a href="http://en.wikipedia.org/wiki/Real_number">real</a>&#8220;. That made my day!</p>
]]></content:encoded>
			<wfw:commentRss>http://tafakuri.net/?feed=rss2&amp;p=76</wfw:commentRss>
		</item>
		<item>
		<title>Presence Africaine</title>
		<link>http://tafakuri.net/?p=74</link>
		<comments>http://tafakuri.net/?p=74#comments</comments>
		<pubDate>Mon, 23 Mar 2009 00:07:44 +0000</pubDate>
		<dc:creator>Mucheru</dc:creator>
		
		<category><![CDATA[filosofia]]></category>

		<guid isPermaLink="false">http://tafakuri.net/?p=74</guid>
		<description><![CDATA[I recently finished reading “The Surreptitious Speech: Présence Africaine and the Politics of Otherness “ , a compilation of essays edited by Valentine Mudimbe. The collection celebrates 40 years of the journal Presence Africaine.
Mudimbe writes a very engaging summary at the end of the compendium, in which he lays the tone of the compilation and [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://books.google.com/books?id=c8YkXIGrm1gC&amp;printsec=frontcover"><img src="http://bks3.books.google.com/books?id=c8YkXIGrm1gC&amp;printsec=frontcover&amp;img=1&amp;zoom=5&amp;sig=ACfU3U0tticKD3abfYRtt5KsUwpXscqymg" title="The Surreptitious Speech: Présence Africaine and the Politics of Otherness ..." alt="The Surreptitious Speech: Présence Africaine and the Politics of Otherness ..." width="51" border="0" height="80" /></a>I recently finished reading “<a href="http://books.google.com/books?id=c8YkXIGrm1gC&amp;printsec=frontcover">The Surreptitious Speech: Présence Africaine and the Politics of Otherness </a>“ , a compilation of essays edited by Valentine Mudimbe. The collection celebrates 40 years of the journal Presence Africaine.</p>
<p>Mudimbe writes a very engaging summary at the end of the compendium, in which he lays the tone of the compilation and gives his opinion on Presence.</p>
<blockquote><p>In the span of forty years, the journal and publishing house Presence Africaine succeeded in organizing a new literary and intellectual space for “a surreptitious speech.” This space is not the other side of what we may call the Western space. In fact, it belongs to that space, though it is true that from the beginning Presence defined itself on the margin of this center it challenged.</p>
<p>There was a political reason for this. André Gide made it explicit in the first issue of the journal: why should Presence speak according to the expectations of a culture that was violating what Presence wanted to promote?</p></blockquote>
<p>The idea of constructing space struck me as a very algebraic operation (we construct spaces in algebra all the time) and that got me excited. Theoretical spaces are a powerful tool for reasoning in Mathematics and in many sciences. How would this tool be useful in such a “concrete” field of study as African Studies?</p>
<p>Mudimbe makes it clear that the space created by Presence is a theoretical space, much like the mathematical one.</p>
<blockquote><p>A space is always a construct. It is a theoretical articulation that claims to render and represent operations or, put simply, the reality of a place, that is, a primary experience. A space is, to say the least, a second-order plane reflecting upon a first-order practice of life and human experience.</p></blockquote>
<p>In algebra, we use spaces to help us model some aspect of reality that is the subject of study. For instance, we may model the forces acting on an object as vectors in a 3 dimensional plane. In doing so we ignore some of the complexities of the real world and escape into the clean world of 3 dimensional vector spaces.</p>
<p>A notable difference between intellectual movement theoretical space and the mathematical modeling space is that the intellectual space influences the reality it models. It seems to be an active space where the mathematical space is passive (the fact that we model some scenario as vectors acting in a 3 dimensional space does not actually mean that it happens that way). In a Schrödinger-esque manner, the fact that an intellectual movement space studies a certain society changes the reality of that society.</p>
<blockquote><p>This second-degree organization, by its very being, considerably alters and transforms the primary logic in which it claims to root itself. To that extent, its narratives as well as its postulations invent “what is really out there’ in the field of everyday place. Methods of faithfully expressing the place, at least in social and human sciences, undergo regular transformations in order to reflect better the reality of an experience and its complexity.</p></blockquote>
<p>The idea of using spaces as a metaphor comes together wonderfully in the next paragraph. Mudimbe makes explicit the contending cultural theoretical spaces and shows how herculean Presence’s task was.</p>
<blockquote><p>Indeed, after reading the contributions to this volume, one could deduce that, until the founding of Presence Africaine, African cultures and their designations were submitted to a European space that actualized them as figures of its own past, precisely as anterior to the rupture that radically separated prehistory from history. The memory of the European space would appear thus, diachronically and synchronically, as the paradigm of human experience and, at the same time, as that which historically has muted all other human differences by reducing them to the project of an evolutionary becoming. In this perspective, Presence Africaine could appear to signify the unthinkable: an otherness spatializing itself from a nowhere that could not but be a utopian project. In effect, its surreptitious voice faces Western culture in the name of an absolute alterity; yet this very alterity seems to spring from the Western space.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://tafakuri.net/?feed=rss2&amp;p=74</wfw:commentRss>
		</item>
		<item>
		<title>A round-up of fun toys and tools</title>
		<link>http://tafakuri.net/?p=72</link>
		<comments>http://tafakuri.net/?p=72#comments</comments>
		<pubDate>Wed, 21 Jan 2009 05:53:50 +0000</pubDate>
		<dc:creator>Mucheru</dc:creator>
		
		<category><![CDATA[teknolojia]]></category>

		<guid isPermaLink="false">http://tafakuri.net/?p=72</guid>
		<description><![CDATA[Microsoft does a poor job of advertising new products, especially those in beta. Often this leads to the impression that Microsoft does not innovate. Here are some new products I’ve been playing with/using that are pretty cool.

PowerPoint Plex (pptPlex) from Office Labs (http://www.officelabs.com/projects/pptPlex/Pages/default.aspx).If you’ve ever wished your slideshow could be something more than a linear [...]]]></description>
			<content:encoded><![CDATA[<p>Microsoft does a poor job of advertising new products, especially those in beta. Often this leads to the impression that Microsoft does not innovate. Here are some new products I’ve been playing with/using that are pretty cool.</p>
<ol>
<li>PowerPoint Plex (pptPlex) from Office Labs (<a href="http://www.officelabs.com/projects/pptPlex/Pages/default.aspx" title="http://www.officelabs.com/projects/pptPlex/Pages/default.aspx">http://www.officelabs.com/projects/pptPlex/Pages/default.aspx</a>).If you’ve ever wished your slideshow could be something more than a linear series of slides, this is for you. You can can arrange your slides on a canvas, zoom in and out of slides and set your path for navigating through the slides. This means that you can have long code samples in your presentations and zoom into the sections you need as you need them. I can’t imagine going back to linear slides.<object height="344" width="425">
<param value="http://www.youtube.com/v/YvsdRFRBxhA&amp;hl=en&amp;fs=1" name="movie"></param>
<param value="true" name="allowFullScreen"></param>
<param value="always" name="allowscriptaccess"></param></object><br />
An earlier version of Plex was used in this demo of Touch Wall.<object height="344" width="425"></p>
<param value="http://www.youtube.com/v/PimbkQNKzb4&amp;hl=en&amp;fs=1" name="movie"></param>
<param value="true" name="allowFullScreen"></param>
<param value="always" name="allowscriptaccess"></param></object></li>
<li>Script# (<a href="http://projects.nikhilk.net/ScriptSharp" title="http://projects.nikhilk.net/ScriptSharp">http://projects.nikhilk.net/ScriptSharp</a>)This is the result of an independent project by Nikhil Kothari, an software architect in the .Net developer platform. Using the script# compiler, you can compile C# code into JavaScript (it does with C# what Google Web Toolkit does for Java). Here’s a blurb from Nikhil’s site.<br />
<blockquote><p>Script# brings productivity to Ajax and JavaScript development. Script# is a free tool that enables developers to author C# source code and subsequently compile it into regular script that works across all modern browsers, and in doing so, leverage the productivity and power of existing .NET tools as well as the Visual Studio IDE. Script# empowers you with a development methodology and approach that brings software engineering, long term maintainability and scalable development approaches for your Ajax applications, components and frameworks.</p></blockquote>
<p>With Script#, you can design your AJAX applications just as you would .Net applications – complete with object inheritance, namespaces, interfaces, event receivers, private methods etc. The JavaScript produced is browser independent, meaning that you get insulated from all the little browser quirks.</p>
<p>You also get a lot of code re-use, just as with regular OOP. Script# provides its own base libraries for Ajax development, mirroring the libraries provided in the .Net platform. Alternatively, you can use base libraries from ASP.Net AJAX, or third party platforms like <a href="http://www.extjs.com/">ExtJS</a>(via <a href="http://code.google.com/p/extsharp/">Ext#</a>). Script# produces a DLL with the JavaScript at compilation so your DLLs can be used in future projects as reference assemblies.</li>
<li>Photosynth(<a href="http://photosynth.net/" title="http://photosynth.net/">http://photosynth.net/</a>)This product from Microsoft Live Labs takes  set of photos and constructs a 3D model. The effects are pretty astounding.<object height="295" width="425">
<param value="http://www.youtube.com/v/p16frKJLVi0&amp;hl=en&amp;fs=1" name="movie"></param>
<param value="true" name="allowFullScreen"></param>
<param value="always" name="allowscriptaccess"></param></object></li>
<li>Songsmith(<a href="http://research.microsoft.com/en-us/um/redmond/projects/songsmith/index.html" title="http://research.microsoft.com/en-us/um/redmond/projects/songsmith/index.html">http://research.microsoft.com/en-us/um/redmond/projects/songsmith/index.html</a>)This falls neatly into the toy category. This product from Microsoft Research allows you to hum or sing a tune and it generates an accompaniment. ArsTechnica had a really good article on <a href="http://arstechnica.com/reviews/apps/microsoft-songsmith-review.ars">this</a>. There are some YouTube videos providing songsmith re-arrangements of classic tunes. My favorite is the remake of Roxanne by the Police.<object height="295" width="425">
<param value="http://www.youtube.com/v/ypycpKQxXR0&amp;hl=en&amp;fs=1" name="movie"></param>
<param value="true" name="allowFullScreen"></param>
<param value="always" name="allowscriptaccess"></param></object></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://tafakuri.net/?feed=rss2&amp;p=72</wfw:commentRss>
		</item>
		<item>
		<title>Summing numbers that cannot be written as a sum of two abundant numbers</title>
		<link>http://tafakuri.net/?p=71</link>
		<comments>http://tafakuri.net/?p=71#comments</comments>
		<pubDate>Fri, 16 Jan 2009 15:08:06 +0000</pubDate>
		<dc:creator>Mucheru</dc:creator>
		
		<category><![CDATA[project euler]]></category>

		<category><![CDATA[hisabati]]></category>

		<guid isPermaLink="false">http://tafakuri.net/?p=71</guid>
		<description><![CDATA[Problem 23 from Project Euler asks for the sum of numbers that cannot be written as the sum of two abundant numbers:
A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + [...]]]></description>
			<content:encoded><![CDATA[<p>Problem 23 from Project Euler asks for the sum of numbers that cannot be written as the sum of two abundant numbers:</p>
<blockquote><p>A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.</p>
<p>A number whose proper divisors are less than the number is called deficient and a number whose proper divisors exceed the number is called abundant.</p>
<p>As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.</p>
<p>Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.</p></blockquote>
<p>When I read the problem, the requirements seemed to be pretty straight-forward. We need to find all the abundant numbers in the range so that we can determine all the numbers that are the sum of two abundant numbers. My first(naive) approach to this was terribly inefficient – it ran for hours and didn’t give the right answer. We cannot get around the need to determine the abundant numbers in range. However, we can reduce the search space when determining the numbers that are expressed as sums of two abundant numbers. To do this, we take advantage of some known properties:</p>
<ol>
<li>The upper bound for numbers that cannot be expressed as the sum of two numbers is actually 20161 not 28123 (as shown <a href="http://projecteuclid.org/DPubS?verb=Display&amp;version=1.0&amp;service=UI&amp;handle=euclid.prims/1195193227&amp;page=record">here</a>).</li>
<li>All even numbers greater than 48 can be written as the sum of two abundant numbers (also shown in the article <a href="http://projecteuclid.org/DPubS?verb=Display&amp;version=1.0&amp;service=UI&amp;handle=euclid.prims/1195193227&amp;page=record">here</a>).</li>
</ol>
<p>With these facts in mind, we are ready for the implementation. The first routine determines the abundant numbers in range.</p>
<pre style="background-color: #fbfbfb; min-height: 40px; width: 488px; font-family: consolas,'Courier New',courier,monospace; height: 236px; overflow: auto; border: #cecece 1px solid; padding: 5px"><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  1: def abundant_nums_in_range(n):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  2:     abundant=[]
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  3:     min_abundant_not_multiple_of_2_or_3 = 5391411025
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  4:     for i in range(1,n):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  5:         if(i <span style="color: #0000ff">&lt;</span> min_abundant_not_multiple_of_2_or_3):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  6:             if(i % 2 != 0) and (i%3 != 0):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  7:                 continue
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  8:         divisors = proper_divisors(i)
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  9:         divisors_sum = 0
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 10:         for d in divisors:
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 11:             divisors_sum += d
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 12:         if(divisors_sum <span style="color: #0000ff">&gt;</span> i):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 13:             abundant.append(i)
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 14:     return abundant
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 15: </span></pre>
<p>The routine that determines proper divisors of a number tests divisors from 1 to the square-root of the number and extracts all divisors in pairs.</p>
<pre style="background-color: #fbfbfb; min-height: 40px; width: 487px; font-family: consolas,'Courier New',courier,monospace; height: 209px; overflow: auto; border: #cecece 1px solid; padding: 5px"><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  1: def proper_divisors(n):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  2:     divisors =[]
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  3:     maxValue = int(sqrt(n))+1
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  4:     for i in range(1,maxValue):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  5:         if(n % i == 0):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  6:             if(i not in divisors):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  7:                 divisors.append(i)
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  8:             quotient = n/i
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  9:             if(quotient == n): continue
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 10:             if(quotient not in divisors):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 11:                 divisors.append(quotient)
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 12:     return divisors
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 13: </span></pre>
<p>The main routine for the solution is the method that determines all numbers in range that can be expressed as sum of two numbers. Since we know that all even numbers greater than 48 are in our target set, we only need to determine odd numbers in range. The only way to get an odd number by summing two numbers is to sum an even number with an odd.</p>
<pre style="background-color: #fbfbfb; min-height: 40px; width: 491px; font-family: consolas,'Courier New',courier,monospace; height: 503px; overflow: auto; border: #cecece 1px solid; padding: 5px"><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  1: def abundant_pair_sums_in_range(n):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  2:     abundant_nums = abundant_nums_in_range(n)
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  3:     even_abundants = []
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  4:     odd_abundants = []
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  5:     for a in abundant_nums:
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  6:         if(a %2 == 0):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  7:             even_abundants.append(a)
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  8:         else:
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  9:             odd_abundants.append(a)
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 10:     abundant_sum_nums = []
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%"> 11:     num_even_abundants = len(even_abundants)
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 12:     num_odd_abundants = len(odd_abundants)
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 13:     curr_num = 0
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 14:     abundant_nums_filter = [0 for x in range(0,n)]
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 15:     # all even numbers &gt;48 are the sum of two abundants
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 16:     for a in range(0,n):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 17:         if(a % 2 ==0):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 18:             if(a &gt; 48):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 19:                 abundant_nums_filter[a] = 1
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 20:             elif a in [24,30,32,36,38,40,42,44,48]:
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 21:                 abundant_nums_filter[a] = 1
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 22:     for i in range(0, num_even_abundants):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 23:         for j in range(0, num_odd_abundants):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 24:             curr_num = even_abundants[i] + odd_abundants[j]
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 25:             if(curr_num <span style="color: #0000ff">&lt;</span> n):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 26:                 abundant_nums_filter[curr_num] = 1
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 27:     for a in range(1,n):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 28:         if abundant_nums_filter[a] == 1 :
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 29:             abundant_sum_nums.append(a)
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 30:     return abundant_sum_nums
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px"> 31: </span></pre>
<p>The last routine ties all operations together.</p>
<pre style="background-color: #fbfbfb; min-height: 40px; width: 493px; font-family: consolas,'Courier New',courier,monospace; height: 133px; overflow: auto; border: #cecece 1px solid; padding: 5px"><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  1: def get_non_abundant_sums(n):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  2:     abundant_sums = abundant_pair_sums_in_range(n)
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%">  3:     sum_all = sum([x for x in range(0,n)])
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  4:     sum_abundants = sum(abundant_sums)
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  5:     return (sum_all - sum_abundants)
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  6: </span></pre>
]]></content:encoded>
			<wfw:commentRss>http://tafakuri.net/?feed=rss2&amp;p=71</wfw:commentRss>
		</item>
		<item>
		<title>Length of cycles in unit fractions</title>
		<link>http://tafakuri.net/?p=69</link>
		<comments>http://tafakuri.net/?p=69#comments</comments>
		<pubDate>Fri, 02 Jan 2009 09:01:30 +0000</pubDate>
		<dc:creator>Mucheru</dc:creator>
		
		<category><![CDATA[project euler]]></category>

		<category><![CDATA[hisabati]]></category>

		<guid isPermaLink="false">http://tafakuri.net/?p=69</guid>
		<description><![CDATA[Problem 26 of Project Euler asks us to find the length of cycles in unit fractions:
A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:



1/2
=
0.5


1/3
=
0.(3)


1/4
=
0.25


1/5
=
0.2


1/6
=
0.1(6)


1/7
=
0.(142857)


1/8
=
0.125


1/9
=
0.(1)


1/10
=
0.1



Where 0.1(6) means 0.166666&#8230;, and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit [...]]]></description>
			<content:encoded><![CDATA[<p>Problem 26 of Project Euler asks us to find the length of cycles in unit fractions:</p>
<blockquote><p>A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:</p>
<blockquote>
<table>
<tr>
<td><sup>1</sup>/<sub>2</sub></td>
<td>=</td>
<td>0.5</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>3</sub></td>
<td>=</td>
<td>0.(3)</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>4</sub></td>
<td>=</td>
<td>0.25</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>5</sub></td>
<td>=</td>
<td>0.2</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>6</sub></td>
<td>=</td>
<td>0.1(6)</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>7</sub></td>
<td>=</td>
<td>0.(142857)</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>8</sub></td>
<td>=</td>
<td>0.125</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>9</sub></td>
<td>=</td>
<td>0.(1)</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>10</sub></td>
<td>=</td>
<td>0.1</td>
</tr>
</table>
</blockquote>
<p>Where 0.1(6) means 0.166666&#8230;, and has a 1-digit recurring cycle. It can be seen that <sup>1</sup>/<sub>7</sub> has a 6-digit recurring cycle.</p>
<p>Find the value of <em>d</em> &lt; 1000 for which <sup>1</sup>/<sub><em>d</em></sub> contains the longest recurring cycle in its decimal fraction part.</p></blockquote>
<p>The decimal representations of the fractions and therefore cycles can be discovered using <a href="http://en.wikipedia.org/wiki/Long_division">long division</a>. A python implementation of this would be:</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 464px; font-family: consolas,'Courier New',courier,monospace; height: 421px"><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  1: <span style="color: #0000ff">def</span> unit_fraction_decimal_rep(n):
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  2:     numerator, denominator = 1, n
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  3:     fraction = []
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  4:     remainders = {}
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  5:     <span style="color: #0000ff">while</span> True:
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  6:         numerator *= 10
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  7:         r = numerator % denominator
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  8:         q = (numerator - r)/denominator
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  9:         <span style="color: #0000ff">if</span>(r == 0):
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 10:             fraction.append(q)
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 11:             <span style="color: #0000ff">break</span>
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 12:         <span style="color: #0000ff">if</span>(r <span style="color: #0000ff">in</span> remainders.values()):
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 13:             foundCycle = False
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 14:             <span style="color: #0000ff">for</span> key,value <span style="color: #0000ff">in</span> remainders.items():
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 15:                 <span style="color: #0000ff">if</span>(r == value) <span style="color: #0000ff">and</span> (q == int(fraction[key])):
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 16:                     # mark the cycle with parenthesis
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 17:                     fraction.insert(key,&#8221;<span style="color: #8b0000">(</span>&#8220;)
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 18:                     fraction.append(&#8221;<span style="color: #8b0000">)</span>&#8220;)
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 19:                     foundCycle = True
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 20:                     <span style="color: #0000ff">break</span>
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 21:             <span style="color: #0000ff">if</span> foundCycle:
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 22:                 <span style="color: #0000ff">break</span>
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 23:         remainders[len(fraction)] = r
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 24:         fraction.append(str(q))
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 25:         numerator = r
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 26:     <span style="color: #0000ff">return</span> &#8220;<span style="color: #8b0000">0.</span>&#8220;+&#8221;<span style="color: #8b0000"></span>&#8220;.<span style="color: #0000ff">join</span>(fraction)</span></pre>
<p>To solve the problem posed, we could generate fractional representations of all integers from 1000 down and check for the longest cycles. However, looking at the algorithm above we can figure out how long a cycle will be without actually having to calculate it.</p>
<p>The terms in the decimal representation are determined by the remainder in each iteration of the loop starting on line 5 above. If one of the remainders is 0, then the fraction terminates. On the other hand, if we see a remainder that we have seen previously, then we have gotten to the end of a cycle. The maximum length of a cycle is n-1 since there are only n-1 possibilities for the remainder.</p>
<p>In each iteration of the loop on line 5, we multiply the numerator by 10. This establishes a relationship between 10 and n. We use a little algebra to explore the relationship.</p>
<ol>
<li>If the fraction terminates, then at some point we get a remainder of 0 meaning that n evenly divides a power of 10. Since 10 has only two divisors, 2 and 5, n evenly divides a power of 10 if and only if n = 2<sup>a</sup>x5<sup>b</sup> where a,b are non-negative integers (a or b may be 0 in which case n is either only divisible by 2 or by 5).</li>
<li>The fraction will recur if n does not evenly divide any power of 10.
<ul>
<li>If n is has no factors in common with 10 (abbreviated as n is <a href="http://en.wikipedia.org/wiki/Coprime">coprime</a> with 10), the length of the recurring cycle is equal to the <a href="http://en.wikipedia.org/wiki/Order_(group_theory)">order</a> of 10 in the group Z<sub>n</sub> (<a href="http://en.wikipedia.org/wiki/Multiplicative_group_of_integers_modulo_n">the multiplicative group of integers modulo n</a>). If 10 has order d then 10<sup>d</sup> mod n = 1 and 1 becomes the first remainder repeated (since we start with numerator 1).</li>
<li>If n is not coprime with 10, there is no d for which 10<sup>d</sup> mod n = 1. In such cases n= 2<sup>a</sup>x5<sup>b</sup>xm where m is coprime with 10. So <sup>1</sup>/<sub>n</sub> = <sup>1</sup>/<sub>(2<sup>a</sup>x5<sup>b</sup>)</sub> x <sup>1</sup>/<sub>m</sub> . The component <sup>1</sup>/<sub>(2<sup>a</sup>x5<sup>b</sup>)</sub> will terminate so the cycle that results in <sup>1</sup>/<sub>n</sub> is contributed by <sup>1</sup>/<sub>m</sub> and the length of the cycle is equal to the order of 10 in Z<sub>m</sub>.</li>
</ul>
</li>
</ol>
<p>Applying this, we come up with the algorithm below:</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 402px; font-family: consolas,'Courier New',courier,monospace; height: 232px"><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  1: <span style="color: #0000ff">def</span> recurring_cycle_length(n):
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  2:     cycleLen = 0
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  3:     #  remove powers of 2 and 5 in n
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  4:     <span style="color: #0000ff">while</span> (n % 2 == 0):
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  5:         n /= 2
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  6:     <span style="color: #0000ff">while</span> (n % 5 == 0):
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  7:         n /= 5
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  8:     <span style="color: #0000ff">if</span> n &gt; 1:
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  9:         a = 10 % n
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 10:         cycleLen = 1
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 11:         <span style="color: #0000ff">while</span> a != 1:
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 12:             a *= 10
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 13:             a %= n
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 14:             cycleLen += 1
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 15:     <span style="color: #0000ff">return</span> cycleLen</span></pre>
<p>The longest cycle will be generated when 10 has a high order in Z<sub>n</sub> or Z<sub>m</sub>. So when searching for the longest cycle we start with the maximum value of n in the range and move down. If n is prime, the order of 10 in the Z<sub>n</sub> is either n-1 or a divisor of n-1. If we find a max value for the period which is equal to n-1, we have found the longest cycle.</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 402px; font-family: consolas,'Courier New',courier,monospace; height: 191px"><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  1: <span style="color: #0000ff">def</span> max_len_recurring_cycle_in_range(n):
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  2:     # iterate from the max num down
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  3:     maxCycleLen = 0
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  4:     maxCycleLenGenerator = n
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  5:     <span style="color: #0000ff">for</span> i <span style="color: #0000ff">in</span> range(n, 1, -1):
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  6:         currLen = recurring_cycle_length(i)
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  7:         <span style="color: #0000ff">if</span>(currLen &gt; maxCycleLen):
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  8:             maxCycleLen = currLen
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  9:             maxCycleLenGenerator = i
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 10:         <span style="color: #0000ff">if</span>(currLen == i-1):
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 11:             <span style="color: #0000ff">break</span>
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 12:     <span style="color: #0000ff">return</span> maxCycleLenGenerator, maxCycleLen</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://tafakuri.net/?feed=rss2&amp;p=69</wfw:commentRss>
		</item>
		<item>
		<title>Project Euler: nth lexicographic permutation</title>
		<link>http://tafakuri.net/?p=68</link>
		<comments>http://tafakuri.net/?p=68#comments</comments>
		<pubDate>Sat, 27 Dec 2008 19:35:22 +0000</pubDate>
		<dc:creator>Mucheru</dc:creator>
		
		<category><![CDATA[project euler]]></category>

		<category><![CDATA[hisabati]]></category>

		<guid isPermaLink="false">http://tafakuri.net/?p=68</guid>
		<description><![CDATA[Problem 24 from Project Euler asks us to find the nth lexicographic permutation of a sequence of digits:
A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. [...]]]></description>
			<content:encoded><![CDATA[<p>Problem 24 from Project Euler asks us to find the nth lexicographic permutation of a sequence of digits:</p>
<blockquote><p>A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:</p>
<p style="text-align: center">012   021   102   120   201   210</p>
<p>What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?</p>
</blockquote>
<p>My first instinct was to write a routine that would enumerate permutations of a sequence of digits in lexicographic order. This would involve two functions: the first would take a permutation and return the next lexicographic permutation of the digits. The next function would call the first 999999 times, starting with the first sequence (0123456789) and passing intermediate values as it went along. The main function is below.</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 459px; font-family: consolas,'Courier New',courier,monospace; height: 338px"><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  1: <span style="color: #0000ff">def</span> next_lexicographic_permutation(currPermutation):
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  2:     digits = [int(x) <span style="color: #0000ff">for</span> x <span style="color: #0000ff">in</span> str(currPermutation)]
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  3:     maxIndex = len(digits)-1
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  4:     prevValue = digits[maxIndex]
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  5:     currValue = digits[maxIndex]
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  6:     for i in range(maxIndex, -1,-1):
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  7:         currValue = digits[i]
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  8:         <span style="color: #0000ff">if</span> (i == 0) and (currValue == max(digits)):
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  9:             <span style="color: #0000ff">return</span> currPermutation
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 10:         <span style="color: #0000ff">if</span>(currValue &lt; prevValue):
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 11:             prefix = digits[:i]
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 12:             suffix = digits[i:]
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 13:             nextStart = min([x <span style="color: #0000ff">for</span> x <span style="color: #0000ff">in</span> suffix <span style="color: #0000ff">if</span> x &gt; currValue])
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 14:             suffix.remove(nextStart)
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 15:             suffix.sort()
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 16:             nextPermutation = &#8220;<span style="color: #8b0000"></span>&#8220;.join([str(x) <span style="color: #0000ff">for</span> x <span style="color: #0000ff">in</span> prefix]) +str(nextStart)+ &#8220;<span style="color: #8b0000"></span>&#8220;.join([str(x) <span style="color: #0000ff">for</span> x <span style="color: #0000ff">in</span> suffix])
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 17:             <span style="color: #0000ff">return</span> nextPermutation
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 18:         <span style="color: #0000ff">else</span>:
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 19:             prevValue = currValue
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 20:     <span style="color: #0000ff">return</span> currPermutation
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 21: </span></pre>
<p>While this approach worked, the operation took 30 seconds. After looking at the problem for a while longer and consulting my muse, I realized that we could directly determine the nth permutation. It is an established fact that given a set of m elements, the number of permutations possible is given by m!( read as &#8220;m factorial&#8221;, defined <a href="http://en.wikipedia.org/wiki/Factorial">here</a>). So for our set of 10 digits, there are 10! possible permutations. Of these permutations, 1/10 of them have 0 as the first digit, 1/10 have 1 as the first etc. This comes to 9! = 362,880 permutations starting with any one digit. Since sequences are presented in lexicographic order, we know that the millionth permutation has to start with 2 ( the sequences starting with 0 end at 9! and those starting with1 end at 2*9!).</p>
<p>The millionth permutation of 10 digits will be the 274240th permutation from the set of permutations starting with 2 (i.e. 1000,000 - 2*9!, accounting for the permutations that started with 0 and 1). The problem of determining the 274240th permutation from the set of permutations starting with 2 is equivalent to the problem we&#8217;ve just solved for 10 digits, except that this time we are determining the  274240th permutation of the digits 013456789. To solve it,we apply the same algorithm as we did previously giving a rather neat recursive solution to the general problem.</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 475px; font-family: consolas,'Courier New',courier,monospace; height: 257px"><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  1: <span style="color: #0000ff">def</span> nth_lexicographic_permutation(initialPermutation, n):
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  2:     currPermutation=str(initialPermutation)
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  3:     <span style="color: #0000ff">if</span> len(currPermutation) == 1: <span style="color: #0000ff">return</span> initialPermutation
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  4:     residue = n
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  5:     # number of permutations starting with any one digit
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  6:     numSuffixPermutations = factorial(len(currPermutation) - 1)
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  7:     outputDigitIndex = 0
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  8:     <span style="color: #0000ff">if</span>(numSuffixPermutations &lt; residue):
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px">  9:         outputDigitIndex = residue / numSuffixPermutations
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 10:         <span style="color: #0000ff">if</span>(residue % numSuffixPermutations == 0):
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 11:             outputDigitIndex -= 1
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 12:         residue -= (outputDigitIndex * numSuffixPermutations)
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 13:     indexDigit = currPermutation[outputDigitIndex]
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 14:     currPermutation = currPermutation.replace(indexDigit,&#8221;)
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 15:     <span style="color: #0000ff">return</span> indexDigit + nth_lexicographic_permutation(currPermutation, residue)
</span><span style="margin: 0em; background-color: #fbfbfb; width: 100%; font-size: 12px"> 16: </span></pre>
<p>This second solution runs in less than a second!</p>
]]></content:encoded>
			<wfw:commentRss>http://tafakuri.net/?feed=rss2&amp;p=68</wfw:commentRss>
		</item>
		<item>
		<title>Project Euler: Determining the number of paths in a grid</title>
		<link>http://tafakuri.net/?p=66</link>
		<comments>http://tafakuri.net/?p=66#comments</comments>
		<pubDate>Tue, 16 Dec 2008 23:07:03 +0000</pubDate>
		<dc:creator>Mucheru</dc:creator>
		
		<category><![CDATA[project euler]]></category>

		<category><![CDATA[hisabati]]></category>

		<guid isPermaLink="false">http://tafakuri.net/?p=66</guid>
		<description><![CDATA[For problem 15 from Project Euler, we are asked to find the number of paths leading from the top left corner of a grid to the bottom right corner that do not involve backtracking.
Starting in the top left corner of a 2 by 2 grid, there are 6 routes (without backtracking) to the bottom right [...]]]></description>
			<content:encoded><![CDATA[<p>For problem 15 from Project Euler, we are asked to find the number of paths leading from the top left corner of a grid to the bottom right corner that do not involve backtracking.</p>
<blockquote><p>Starting in the top left corner of a 2 by 2 grid, there are 6 routes (without backtracking) to the bottom right corner.</p>
<p style="text-align: center; display: block; height: 151px"><img src="http://projecteuler.net/project/images/p_015.gif" /></p>
<p>How many routes are there through a 20 by 20 grid?</p></blockquote>
<p>Given the way the problem is set up, for any one node there are at most two nodes that can lead directly to the node. If we assign coordinates to the grid with (0,0) being the top-left corner, the only nodes that have direct outgoing paths to (1,1) are (0,1) and (1,0). Trivially, each of the nodes in row 0 (the top row) has only one node that can lead into it (the node immediately to its left). Similarly, nodes in column 0 (the left-most column) each have one node leading into them (the node immediately above them). We can use this information to calculate the number of paths ending at any one node in the grid.</p>
<p>To do this, consider nodes A, B and C that form the lower right triangle of an arbitrary square in the grid. We label the nodes such that A is the lower left corner of the square, B is the lower right corner while C is the upper right corner of the square. As shown before, the only way to get a path ending at B is to take a path ending at either A or C and add one step to it. We also know that a path ending at A cannot pass through C (since we don&#8217;t allow backtracking) and similarly a path ending at C cannot pass through A. Therefore, the number of paths ending at B = number of paths ending at A + number of paths ending at C.</p>
<p>This leads to the following rather short algorithm:</p>
<pre style="background-color: #fbfbfb; min-height: 40px; width: 449px; font-family: consolas,'Courier New',courier,monospace; height: 128px; overflow: auto; border: #cecece 1px solid; padding: 5px"><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  1: <span style="color: #0000ff">def</span> num_paths_to_grid_bottom(numRowCells, numColumnCells):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  2:     currRow = [1 for x in range(numColumnCells + 1)]
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  3:     # the number of nodes to consider = numRowCells + 1
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  4:     for numRow in range(1, numRowCells + 1):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  5:         for numColumn in range(1, numColumnCells + 1):
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  6:             currRow[numColumn] += currRow[numColumn - 1]
</span><span style="background-color: #fbfbfb; margin: 0em; width: 100%; font-size: 12px">  7:     <span style="color: #0000ff">return</span> currRow[numColumnCells]</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://tafakuri.net/?feed=rss2&amp;p=66</wfw:commentRss>
		</item>
	</channel>
</rss>
