<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>

		<title><![CDATA[jtolds.com - Math]]></title>
		<link><![CDATA[http://www.jtolds.com/newsletter/category/math]]></link>
		<description><![CDATA[JT Olds' RSS Feed for Math]]></description>

		<language>en-us</language>
		<copyright>Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License</copyright>

<item>
	<title><![CDATA[Erdos number idea]]></title>
	<author>JT Olds</author>

	<link><![CDATA[http://www.jtolds.com/newsletter/2008/11/8/erdos-number-idea]]></link>

	<guid>1226131038</guid>
	<pubDate>Sat, 8 Nov 2008 07:57:18 +0000</pubDate>
	<description><![CDATA[<p>One of the ways to know how high up on the famous-mathematician ladder you are is to calculate your <a href="http://en.wikipedia.org/wiki/Erd%C5%91s_number">Erdos number</a>. <a href="http://en.wikipedia.org/wiki/Paul_Erd%C5%91s">Paul Erdos</a> was certainly very prolific. It's basically a game of <a href="http://en.wikipedia.org/wiki/Bacon_number">six degrees of Kevin Bacon</a>, but played with math papers, instead of movies, and Paul Erdos, instead of Kevin Bacon.</p><p>What's interesting (or not, if you realize the ubiquity of <a href="http://en.wikipedia.org/wiki/Small-world_network">small-world networks</a>), is that the median Erdos number is just 5. What's even more interesting is that, according to the <a href="http://www.oakland.edu/enp/trivia.html">Erdos number project</a>, the highest finite Erdos number is 13, with only 5 people in that state!</p><p>So, here's a more interesting game to me. Obviously I can't get an Erdos number of 1 (Paul Erdos is no longer among the living), and I suppose if I really dedicated my academic life to such a pursuit, I could probably reach some similarly low number. However, my interest sort of tapers off near the median (5), since that's common. What would be better is to be the 1 person with an Erdos number of 14! So, I need to write a paper with one of the 5 people that have an Erdos number of 13. More importantly, once I do, I can't screw it up by writing any papers with anyone else. Furthermore, I should probably choose the least likely of those 5 people to write more papers. I suppose I still have the problem in which any sub-path in the supposed full path from me to Erdos might collapse into a smaller path at any time. Hmm.</p><p>On a slightly related note, another interesting dimension is the intersection of the <a href="http://en.wikipedia.org/wiki/Erd%C5%91s-Bacon_number">Erdos number and the Bacon number</a>. The Erdos-Bacon number is the sum of someone's Erdos number and their Bacon number. Evidently the lowest such number is 3. <a href="http://en.wikipedia.org/wiki/Erd%C5%91s-Bacon_number#Methodology">Wikipedia's discussion</a> on how a lower number could be achieved is hilarious. Observe:</p><pre>The only way a lower number could be achieved would be:

    * for an individual who had co-authored an academic paper with Paul Erdos to
      appear in a movie with Kevin Bacon;
    * for Bacon to co-author an academic paper with someone with an Erdos number
      of 1, which would give Bacon an Erdos-Bacon number of 2;
    * for anyone who appeared in the documentary N is a Number along with Erdos
      to appear in a film with Bacon, which would posthumously give Erdos an
      Erdos-Bacon number of 2;
    * for Kevin Bacon to appear in a film that also uses stock footage of Erdos,
      giving Erdos an Erdos-Bacon number of 1;
    * for a heretofore unknown joint academic paper by Bacon and Erdos to be
      published, giving Bacon an Erdos-Bacon number of 1.</pre>]]></description>
</item>

<item>
	<title><![CDATA[Primes]]></title>
	<author>JT Olds</author>

	<link><![CDATA[http://www.jtolds.com/newsletter/2008/10/7/primes]]></link>

	<guid>1223419334</guid>
	<pubDate>Tue, 7 Oct 2008 22:42:14 +0000</pubDate>
	<description><![CDATA[<i><b>Update:</b> oh my goodness. Had I just checked the Wikipedia's entry on <a href="http://en.wikipedia.org/wiki/Prime_number">Prime numbers</a>, I would have been reminded of fundamental things like the <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Sieve of Eratosthenes</a> (which I of course forgot the name of, but the idea is featured prominently in my parallel algorithm, mentioned at the end of this post) and <a href="http://en.wikipedia.org/wiki/Sieve_of_Atkin">newer improvements</a>, along with a <a href="http://en.wikipedia.org/wiki/Prime_gap">ton</a> <a href="http://en.wikipedia.org/wiki/Formula_for_primes">of</a> <a href="http://en.wikipedia.org/wiki/Primality_test">other</a> <a href="http://en.wikipedia.org/wiki/Prime-counting_function">incredibly</a> <a href="http://en.wikipedia.org/wiki/Prime_number_theorem">interesting</a> <a href="http://en.wikipedia.org/wiki/Perfect_number#Even_perfect_numbers">facts</a>, and even <a href="http://en.wikipedia.org/wiki/Image:PrimeNumbersSmall.png">a prime distribution graph</a>! Wikipedia: +1, JT: 0</i><br/>
<br/>
In my static analysis meeting, my professor noted that indexing objects with primes led to an easy set representation scheme, in which a set is simply the product of the prime indices of the objects in that set. I thought this was pretty fascinating. I started wondering about the constraints involved in implementing such a scheme, and inevitably this led to rather worthless homework procrastination that I thought I'd share here.<br/>
<br/>
First, I was curious about prime number distribution over the integers. I made a graph.<br/>
<img src="/newsletter/images/primes.png" alt="prime number distribution"/><br/>
So, how to read the graph. At a given point X on the X-axis, there are Y-many prime numbers within a diameter of 100,000 around X. In other words, it's a histogram of all primes below 1 billion, displayed with 10,000 buckets.<br/>
<br/>
Second (but really a prerequisite to the first), I wrote a program to quickly generate as many prime numbers as possible.<br/>
<style type="text/css"><!-- /**  * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann  * (http://qbnz.com/highlighter/ and http://geshi.org/)  */ .cpp.cpp .de1, .cpp.cpp .de2 {font-family: monospace; font-weight: normal; font-style: normal; margin:0; padding:0; background:inherit;color: #000020;} .cpp.cpp  {font-family:monospace;color: #006; border: 1px solid #d0d0d0; background-color: #f0f0f0; font-size: 10px; padding-left: 20px;} .cpp.cpp a:link {color: #000060;} .cpp.cpp a:hover {background-color: #f0f000;} .cpp.cpp .imp {font-weight: bold; color: red;} .cpp.cpp li, .cpp.cpp .li1 {font-weight: normal;font: normal normal 130% 'Courier New', Courier, monospace; color: #003030;} .cpp.cpp .ln {width:1px;font-weight: normal;text-align:right;margin:0;padding:0 2px;} .cpp.cpp .li2 {font-weight: bold;font-weight: bold; color: #006060;} .cpp.cpp .kw1 {color: #0000ff;} .cpp.cpp .kw2 {color: #0000ff;} .cpp.cpp .kw3 {color: #0000dd;} .cpp.cpp .kw4 {color: #0000ff;} .cpp.cpp .co1 {color: #666666;} .cpp.cpp .co2 {color: #339900;} .cpp.cpp .coMULTI {color: #ff0000; font-style: italic;} .cpp.cpp .es_h {color: #666666; font-weight: bold;} .cpp.cpp .br0 {color: #008000;} .cpp.cpp .sy0 {color: #008000;} .cpp.cpp .sy1 {color: #000080;} .cpp.cpp .sy2 {color: #000040;} .cpp.cpp .sy3 {color: #000040;} .cpp.cpp .sy4 {color: #008080;} .cpp.cpp .st0 {color: #FF0000;} .cpp.cpp .nu0 {color: #0000dd;} .cpp.cpp .nu6 {color: #208080;} .cpp.cpp .nu8 {color: #208080;} .cpp.cpp .nu12 {color: #208080;} .cpp.cpp .nu16 {color:#800080;} .cpp.cpp .nu17 {color:#800080;} .cpp.cpp .nu18 {color:#800080;} .cpp.cpp .nu19 {color:#800080;} .cpp.cpp .me1 {color: #007788;} .cpp.cpp .me2 {color: #007788;} .cpp.cpp span.xtra { display:block; }  --!></style><div class="cpp cpp" style="font-family:monospace;color: #006; border: 1px solid #d0d0d0; background-color: #f0f0f0;"><ol><li class="li1"><div class="de1"><span class="co2">#include &lt;iostream&gt;</span></div></li> <li class="li1"><div class="de1"><span class="co2">#include &lt;list&gt;</span></div></li> <li class="li1"><div class="de1"><span class="co2">#include &lt;math.h&gt;</span></div></li> <li class="li1"><div class="de1">&nbsp;</div></li> <li class="li2"><div class="de2"><span class="kw2">using</span> <span class="kw2">namespace</span> std;</div></li> <li class="li1"><div class="de1">&nbsp;</div></li> <li class="li1"><div class="de1"><span class="co1">// this program finds all primes &lt;= 1 billion and notes if they are twin primes</span></div></li> <li class="li1"><div class="de1"><span class="co1">// or mersenne primes.</span></div></li> <li class="li1"><div class="de1"><span class="co1">// compile with (on JT's machine):</span></div></li> <li class="li2"><div class="de2"><span class="co1">// g++ -O3 -march=nocona -funroll-loops -fomit-frame-pointer -o primes primes.cpp</span></div></li> <li class="li1"><div class="de1">&nbsp;</div></li> <li class="li1"><div class="de1"><span class="kw2">inline</span> <span class="kw4">bool</span> is_mersenne<span class="br0">&#40;</span><span class="kw4">int</span> val<span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; <span class="co1">// so, ((x - 1) &amp; x) is 0 iff x is a power of 2. See</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; <span class="co1">// http://www.cprogramming.com/tutorial/powtwosol.html for a good</span></div></li> <li class="li2"><div class="de2">&nbsp; &nbsp; <span class="co1">// explanation of why.</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; <span class="co1">// since a mersenne number should be of the form 2^n - 1,</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; <span class="co1">// (x &amp; (x + 1)) is 0 iff x is of form 2^n - 1.</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="sy3">!</span><span class="br0">&#40;</span>val <span class="sy3">&amp;</span> <span class="br0">&#40;</span>val <span class="sy2">+</span> <span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div></li> <li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li> <li class="li2"><div class="de2">&nbsp;</div></li> <li class="li1"><div class="de1"><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="kw4">int</span> argc, <span class="kw4">char</span><span class="sy2">**</span> argv<span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li> <li class="li1"><div class="de1">&nbsp;</div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; <span class="co1">// strategy: as we calculate primes, we store previous primes so we only</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; <span class="co1">// have to check divisibility by known primes to know if a new number is</span></div></li> <li class="li2"><div class="de2">&nbsp; &nbsp; <span class="co1">// prime.</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; list<span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span> primes;</div></li> <li class="li1"><div class="de1">&nbsp;</div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; <span class="co1">// largest_number is the largest number we will check for primeness</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw4">int</span> largest_number <span class="sy1">=</span> <span class="nu0">1000000000</span>; <span class="co1">// 1,000,000,000</span></div></li> <li class="li2"><div class="de2">&nbsp;</div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; <span class="co1">// we use last_prime to keep track of if the last number we checked was</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; <span class="co1">// prime. this way determining twin primes is easy.</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw4">bool</span> last_prime <span class="sy1">=</span> <span class="kw2">true</span>;</div></li> <li class="li1"><div class="de1">&nbsp;</div></li> <li class="li2"><div class="de2">&nbsp; &nbsp; <span class="co1">// book keeping</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw4">bool</span> factor_found;</div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw4">int</span> sqrt_i;</div></li> <li class="li1"><div class="de1">&nbsp;</div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; <span class="co1">// we start with 3, because 3 and every prime after is odd, so we can</span></div></li> <li class="li2"><div class="de2">&nbsp; &nbsp; <span class="co1">// increment by 2.</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; primes.<span class="me1">push_back</span><span class="br0">&#40;</span><span class="nu0">2</span><span class="br0">&#41;</span>;</div></li> <li class="li1"><div class="de1">&nbsp;</div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; <span class="co1">// we don't check divisibility by 1, since that's always true, but we output</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; <span class="co1">// it and 2 as primes.</span></div></li> <li class="li2"><div class="de2">&nbsp; &nbsp; <span class="kw3">cout</span> <span class="sy1">&lt;&lt;</span> <span class="nu0">1</span> <span class="sy1">&lt;&lt;</span> endl <span class="sy1">&lt;&lt;</span> <span class="nu0">2</span> <span class="sy1">&lt;&lt;</span> endl;</div></li> <li class="li1"><div class="de1">&nbsp;</div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; <span class="co1">// check all odd numbers starting with 3 until the largest number</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i <span class="sy1">=</span> <span class="nu0">3</span>; i <span class="sy1">&lt;=</span> largest_number; i <span class="sy2">+</span><span class="sy1">=</span> <span class="nu0">2</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li> <li class="li1"><div class="de1">&nbsp;</div></li> <li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// no factor found yet.</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; factor_found <span class="sy1">=</span> <span class="kw2">false</span>;</div></li> <li class="li1"><div class="de1">&nbsp;</div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// store the square root of the number we're checking. we only need to</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// check up to the square root.</span></div></li> <li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; sqrt_i <span class="sy1">=</span> <span class="kw3">sqrt</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span>;</div></li> <li class="li1"><div class="de1">&nbsp;</div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// iterate over previously calculated primes up to the square root of</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// the current number. note that we don't have to check if the iterator</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// is primes.end() because there is empirically always a prime stored</span></div></li> <li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// greater than the square root of the number we're checking. i wonder</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// if there's a proof there. it would probably be of the form of proving</span></div></li><li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// that for a given integer i, a prime p exists such that i &lt;= p &lt; i^2</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span>list<span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span><span class="sy4">::</span><span class="me2">iterator</span> it<span class="br0">&#40;</span>primes.<span class="me1">begin</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; <span class="sy2">*</span>it <span class="sy1">&lt;=</span> sqrt_i; <span class="sy2">++</span>it<span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li> <li class="li1"><div class="de1">&nbsp;</div></li> <li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// if the current number mod the current prime is 0, we found a</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// factor. not prime, break out.</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>i <span class="sy2">%</span> <span class="sy2">*</span>it <span class="sy1">==</span> <span class="nu19">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; factor_found <span class="sy1">=</span> <span class="kw2">true</span>;</div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div></li> <li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li> <li class="li1"><div class="de1">&nbsp;</div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li> <li class="li1"><div class="de1">&nbsp;</div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="sy3">!</span>factor_found<span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li> <li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// no factor found! this is a prime. store it.</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; primes.<span class="me1">push_back</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span>;</div></li> <li class="li1"><div class="de1">&nbsp;</div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// output the prime</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> <span class="sy1">&lt;&lt;</span> i;</div></li> <li class="li2"><div class="de2">&nbsp;</div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// is it a twin prime? (p, p+2)</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>last_prime<span class="br0">&#41;</span> <span class="kw3">cout</span> <span class="sy1">&lt;&lt;</span> <span class="st0">&quot; *&quot;</span>;</div></li> <li class="li1"><div class="de1">&nbsp;</div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// is it a mersenne prime? (2^n-1)</span></div></li> <li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>is_mersenne<span class="br0">&#40;</span>i<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw3">cout</span> <span class="sy1">&lt;&lt;</span> <span class="st0">&quot; +&quot;</span>;</div></li> <li class="li1"><div class="de1">&nbsp;</div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> <span class="sy1">&lt;&lt;</span> endl;</div></li> <li class="li1"><div class="de1">&nbsp;</div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// keep track of if the last number was prime.</span></div></li> <li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last_prime <span class="sy1">=</span> <span class="kw2">true</span>;</div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span></div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last_prime <span class="sy1">=</span> <span class="kw2">false</span>;</div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li> <li class="li1"><div class="de1">&nbsp;</div></li> <li class="li2"><div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span></div></li> <li class="li1"><div class="de1">&nbsp;</div></li> <li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu19">0</span>;</div></li> <li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li> </ol></div><br/>
Third, I think I've figured out a pretty clever algorithm for parallelizing the computation of primes. I was inspired by <a href="http://en.wikipedia.org/wiki/Software_pipelining">software pipelining</a>. I hope to actually write up the algorithm to check its effectiveness. I'll post it here if/when I do. Stay tuned.]]></description>
</item>

<item>
	<title><![CDATA[Can you make (10, 10, 9, 9, 1, +, /, -, *) = 4?]]></title>
	<author>JT Olds</author>

	<link><![CDATA[http://www.jtolds.com/newsletter/2008/8/24/can-you-make-10-10-9-9-1-4]]></link>

	<guid>1219617348</guid>
	<pubDate>Sun, 24 Aug 2008 22:35:48 +0000</pubDate>
	<description><![CDATA[Last night at around 10pm I got a text from my friend asking how you could combine the numbers 10, 10, 9, 9, and 1, to make 4, using only addition, subtraction, multiplication, and/or division, provided you use the numbers once each.<br/>
<br/>
I didn't start thinking about it until later (sorry Mike), but at 2 am I got frustrated and, as I seem wont to do, ignored reasonable priorities (such as sleeping, or the rest of my todo list) and sat down to write a program to enumerate all of the possible solutions, instead of just figuring it out myself.<br/>
<br/>
Below is a general solution to the problem. The basic idea is it is simple enough to consider all the valid possible combinations of the numbers in Reverse Polish Notation (<a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation">postfix</a>), and evaluate them.<br/>
<br/>
<code>#!/usr/bin/python2.5<br/>
<br/>
def add(x, y): return x + y<br/>
def sub(x, y): return x - y<br/>
def mult(x, y): return x * y<br/>
def div(x, y): return float(x) / y<br/>
OPERATIONS = [add, sub, mult, div]<br/>
REPRESENTATIONS = {add: '+', sub: '-', mult: '*', div: '/'}<br/>
<br/>
def new_counter(counter, item):<br/>
&nbsp; if counter is None: counter = 0<br/>
&nbsp; if type(item) == type(add): return counter - 1<br/>
&nbsp; else: return counter + 1<br/>
<br/>
def valid_perms(lst, counter=None):<br/>
&nbsp; if counter is not None and counter <= 0: return<br/>
&nbsp; if len(lst) == 1:<br/>
&nbsp; &nbsp; if new_counter(counter, lst[0]) != 1: return<br/>
&nbsp; &nbsp; yield lst<br/>
&nbsp; else:<br/>
&nbsp; &nbsp; for i in xrange(len(lst)):<br/>
&nbsp; &nbsp; &nbsp; for perm in valid_perms(lst[:i]+lst[i+1:], new_counter(counter, lst[i])):<br/>
&nbsp; &nbsp; &nbsp; &nbsp; yield [lst[i]] + perm<br/>
<br/>
def all_combos(lst, n):<br/>
&nbsp; if n == 0: yield []<br/>
&nbsp; else:<br/>
&nbsp; &nbsp; for i in xrange(len(lst)):<br/>
&nbsp; &nbsp; &nbsp; for combo in all_combos(lst, n - 1):<br/>
&nbsp; &nbsp; &nbsp; &nbsp; yield [lst[i]] + combo<br/>
<br/>
def uniq(generator):<br/>
&nbsp; seen_things=set([])<br/>
&nbsp; for thing in generator:<br/>
&nbsp; &nbsp; if tuple(thing) not in seen_things:<br/>
&nbsp; &nbsp; &nbsp; seen_things.add(tuple(thing))<br/>
&nbsp; &nbsp; &nbsp; yield thing<br/>
<br/>
def valid_postfix_stacks(nums):<br/>
&nbsp; for op_combo in uniq(all_combos(OPERATIONS, len(nums) - 1)):<br/>
&nbsp; &nbsp; for perm in uniq(valid_perms(nums + op_combo)):<br/>
&nbsp; &nbsp; &nbsp; yield perm<br/>
<br/>
def compute(stack):<br/>
&nbsp; s = []<br/>
&nbsp; for item in stack:<br/>
&nbsp; &nbsp; if type(item) != type(add):<br/>
&nbsp; &nbsp; &nbsp; s.append(item)<br/>
&nbsp; &nbsp; else:<br/>
&nbsp; &nbsp; &nbsp; s.append(item(*reversed([s.pop(), s.pop()])))<br/>
&nbsp; assert len(s) == 1<br/>
&nbsp; return s[0]<br/>
<br/>
def infix(stack):<br/>
&nbsp; s = []<br/>
&nbsp; for item in stack:<br/>
&nbsp; &nbsp; if type(item) != type(add):<br/>
&nbsp; &nbsp; &nbsp; s.append(str(item))<br/>
&nbsp; &nbsp; else:<br/>
&nbsp; &nbsp; &nbsp; s.append('(' + ' '.join(reversed([s.pop(), REPRESENTATIONS[item], s.pop()])) + ')')<br/>
&nbsp; assert len(s) == 1<br/>
&nbsp; return s[0]<br/>
<br/>
def main(nums, answer, tol=.0001, find_all=False):<br/>
&nbsp; print "searching..."<br/>
&nbsp; found = False<br/>
&nbsp; for postfix_stack in uniq(valid_postfix_stacks(nums)):<br/>
&nbsp; &nbsp; try: val = compute(postfix_stack)<br/>
&nbsp; &nbsp; except ZeroDivisionError, e: continue<br/>
&nbsp; &nbsp; if (float(val) + float(tol)/2 >= float(answer) and float(val) - float(tol)/2 <= float(answer)):<br/>
&nbsp; &nbsp; &nbsp; print infix(postfix_stack), '=', val<br/>
&nbsp; &nbsp; &nbsp; found = True<br/>
&nbsp; &nbsp; &nbsp; if not find_all: break<br/>
&nbsp; if not found: print "no solutions found"<br/>
 <br/>
if __name__ == "__main__":<br/>
&nbsp; main([10,10,9,9,1], 4)</code>]]></description>
</item>

<item>
	<title><![CDATA[Horseshoe Update]]></title>
	<author>JT Olds</author>

	<link><![CDATA[http://www.jtolds.com/newsletter/2007/4/19/horseshoe-update]]></link>

	<guid>1176966832</guid>
	<pubDate>Thu, 19 Apr 2007 07:13:52 +0000</pubDate>
	<description><![CDATA[The story, thus far:<br/>
<a href="http://www.jtolds.com/newsletter/?id=1140151291">My original writeup</a><br/>
<a href="http://www.jtolds.com/projects/#1176592953">Progress, a year later</a><br/>
<br/>
Today (and by today, I mean 4/18), I presented some aspects of this thing I've started at an undergraduate symposium.<br/>
<br/>
<a href="http://www.cs.umn.edu/~olds/horseshoe.pdf">Check out my poster!</a> <img src="/images/pdf16.gif" alt="[pdf]"/><br/>
(warning, while the file isn't too large, the poster dimensions are)]]></description>
</item>

<item>
	<title><![CDATA[Horseshoe]]></title>
	<author>JT Olds</author>

	<link><![CDATA[http://www.jtolds.com/newsletter/2006/2/17/horseshoe]]></link>

	<guid>1140151291</guid>
	<pubDate>Fri, 17 Feb 2006 04:41:31 +0000</pubDate>
	<description><![CDATA[While bored in Numerical Computing class today, I was lamenting the sad state of pencil and paper games.<br/>
<br/>
I mean, it is sad. Tic-Tac-Toe? You can either win or draw every time if you know what you're doing. And there aren't many permutations of the game to play. Tic-Tac-Toe is fun when you're 3 or really, really bored.<br/>
<br/>
There's also Nim. I don't have cool graphics to describe it, but you can visit <a href="http://www.csm.astate.edu/Nim.html">this page</a> for more information. I mean, it's okay, but it has the same triviality problem as Tic-Tac-Toe.<br/>
<br/>
For a while during high school, I was pretty enthralled with 3x3x3 3D Tic-Tac-Toe, until <lj user="wellofsilence"> pointed out that the game is, in fact, usually over after 3 or 4 turns, and is rather devoid of strategy due to so many possible ways to win. It appears that most prefer the 4x4x4 version, but by this point I had already made two 3D 3x3x3 Tic-Tac-Toe boards (one out of plexiglass and cardboard, and one out of ceramic material). Yeah, so that game let me down. All the same, it's perfectly playable on paper with a pencil.<br/>
<br/>
Lastly, there's that game where you have a grid of dots and you try to make more boxes than your opponent by adding one line at a time, but that game takes a while to set up, and has a significant ramp up time before it is fun (usually when the board is close to full is the game actually fun).<br/>
<br/>
So, I have these 3D Tic-Tac-Toe boards collecting dust, and on top of that I'm bored, and can't think of any fun pencil and paper games. This completely ignores the need for someone to play them with, but all the same. I sat there and thought of possible new games that could solve both of my prior problems.<br/>
<br/>
I thought of one that due to naming cleverness I'm calling <i>horseshoe</i>, and I can't tell if it's trivial in the same way as Tic-Tac-Toe or not, so I thought I'd post it here and request <a href="/contact/">feedback</a>.<br/>
<br/>
<h1>Horseshoe</h1>The game is essentially 3D 3x3x3 Tic-Tac-Toe, but you aren't trying to get 3 in a row. Instead, you're trying to get a horseshoe shape. I've gone completely out of my way with this entry and made tons of graphics (graphics all made by me with the <a href="http://www.gimp.org">GIMP</a>) that will come in a sec, probably prematurely, as I haven't even played it with someone more than once. This was the same problem that I had with the 3D Tic-Tac-Toe. I ran off and made boards before I played it much. But so anyway.<br/>
<br/>
You play it on paper, or whatever, but you have 3 layers of a cube. Each player alternates placing their symbol ('X' or 'O', for example. I use blue and red dots) until the board is full or one player gets a horseshoe. The horseshoe shape looks like the following:<br/>
<br/>
<b><code>XXX<br/>
X X</code></b><br/>
<br/>
It could be 'O's instead of 'X's but you get the idea. Diagonal anything isn't allowed.<br/>
<br/>
I have 4 game samples (each column of squares is one board):<br/>
<img src="/newsletter/images/horseshoe/gamesamples.png" /><br/>
In the first and third games (first and third columns), blue wins, and in the second and fourth, red wins.<br/>
<br/>
To help visualize the winning organizations/layouts/positions, I've also made 3D representations of the winning layouts of each of the four sample games above. I can't think of any winning layouts that aren't just 3D rotations of these four.<br/>
<br/>
<table><tr><td>1st game:<br/>
<img src="/newsletter/images/horseshoe/cube1.png" /></td><td>2nd game:<br/>
<img src="/newsletter/images/horseshoe/cube2.png" /></td></tr><tr><td>3rd game:<br/>
<img src="/newsletter/images/horseshoe/cube3.png" /></td><td>4th game:<br/>
<img src="/newsletter/images/horseshoe/cube4.png" /></td></tr></table><br/>
Anyway, it's not too complicated once you figure out what's going on. The 8 or so times I played against myself it was far less trivial than Tic-Tac-Toe, but maybe that was simply a product of me playing an unfamiliar game against myself :).<br/>
<br/>
I took a picture of the configuration of the first time (to my knowledge) this game has been played with two people (how it's supposed to be played):<br/>
<br/>
<img src="/newsletter/images/horseshoe/horseshoewritten.png" /><br/>
<br/>
'O's won.<br/>
<br/>
Yeah, I don't know what to make of it, as no one actually seems interested in playing me, so I don't know how effective of a pencil and paper game it is.<br/>
<br/>
<a href="/contact/">Let me know</a> what you think.]]></description>
</item>

	</channel>
</rss>
