<br><div>Hey Jeff, your question about controlling the turtle's screen </div><div>might have been just the ticket in my attempts to control</div><div>chaos, namely G. Lingl's chaos.py, which demonstrates </div><div>
sensitivity to initial conditions is a plus if you want your </div><div>algebra to stay on the same page as itself, per equalities</div><div>that won't be equal in the real world. I'm hoping to throw </div><div>that into site-packages on the back end at OST, along with</div>
<div>all those baseball stats in SQL. It's all done with turtles</div><div>(Gregor's thing) and is brilliant, here's a link:</div><div><br></div><div><a href="http://www.4dsolutions.net/ocn/python/OST/chaos.py">http://www.4dsolutions.net/ocn/python/OST/chaos.py</a></div>
<div><br></div><div>What I've been up to lately, besides teaching Python 24/7,</div><div>is debating with the Egyptologists whether computer science</div><div>really has an algorithm for "Egyptian Fractions". Milo is </div>
<div>arguing it doesn't and the consensus seems to be with </div><div>him for now. Fibonacci published what's today often called</div><div>"the greedy algorithm" (though that's more the genre than</div>
<div>the specimen) and I'm including that in Python below.</div><div><br></div><div>At first I though my job would be to subclass the Fraction</div><div>class in fractions, delegating the nuts and bolts to an </div><div>
internal Fraction but adding this .egyptian( ) method</div><div>(really pseudo). With that in mind, I storyboarded this</div><div>science fiction session (not yet real) which is another </div><div>way of saying I applied the "agile" principle of "test </div>
<div>driven development":</div><div><br></div><div><span class="Apple-style-span" style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><div><font face="'courier new', monospace">>>> from unitfractions import Fraction</font></div>
<div><font face="'courier new', monospace">>>> p = Fraction(5,121)</font></div><div><font face="'courier new', monospace">>>> type(p)</font></div><div><font face="'courier new', monospace"><class 'unitfractions.Fraction'></font></div>
<div><font face="'courier new', monospace">>>> p</font></div><div><font face="'courier new', monospace">Fraction(5, 121)</font></div><div><font face="'courier new', monospace">>>> r = p.egyptian( ) # pseudo-egyptian results of Fibonacci-published algorithm</font></div>
<div><font face="'courier new', monospace">>>> r </font></div><div><font face="'courier new', monospace">(Fraction(1,25), Fraction(1,757), Fraction(1,763309), Fraction(1,873960180913), Fraction(1,1527612795642093418846225))</font></div>
<div><font face="'courier new', monospace">>>> sum(r)</font></div><div><font face="'courier new', monospace">Fraction(5, 121)</font></div></span></div><div><br></div><div>I later decided there was no point trying to maintain the</div>
<div>appearance of a whole new class, and that existing</div><div>Fraction objects should just be fed to this greedy algorithm </div><div>directly, giving a tuple of Fraction outputs. </div><div><br></div><div>Not much code involved. Keep it Simple (another </div>
<div>"agile" precept).</div><div><br></div><div>From the original thread:</div><div><br></div><div>"""</div><div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">
On second thought, I think subclassing a fractions.Fraction is overkill. As soon</div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">as said subclass participates in numeric relations with its fellow Fractions</div>
<div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">(of the ordinary kind), it's going to spawn ordinary Fractions (ancestor class). </div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">
<br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">Maintaining an entirely new type just for this one feature is not worth the effort, </div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">
given likely arithmetic relations with peers.</div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">
Also, I'm not a huge fan of recursion where iteration is just as straightforward.</div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">In the case of Fibonacci's greedy algorithm, there's like nothing to it:</div>
<div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><div><font face="'courier new', monospace" size="4">"""</font></div>
<div><font face="'courier new', monospace" size="4">OST Skunkworks:</font></div><div><font face="'courier new', monospace" size="4">Pseudo-Egyptian Fractions</font></div><div><font face="'courier new', monospace" size="4">See:</font></div>
<div class="im" style="color: rgb(80, 0, 80); "><div><font face="'courier new', monospace" size="4"><a href="http://scienceblogs.com/goodmath/2006/11/egyptian_fractions.php" target="_blank" style="color: rgb(0, 0, 204); ">http://scienceblogs.com/goodmath/2006/11/egyptian_fractions.php</a></font></div>
</div><div><font face="'courier new', monospace" size="4"><a href="http://groups.google.com/group/mathfuture/browse_thread/thread/97511940cccd5016?hl=en" target="_blank" style="color: rgb(0, 0, 204); ">http://groups.google.com/group/mathfuture/browse_thread/thread/97511940cccd5016?hl=en</a></font></div>
<div><font face="'courier new', monospace" size="4">"""</font></div><div><font face="'courier new', monospace" size="4"><br></font></div><div><font face="'courier new', monospace" size="4">from fractions import Fraction</font></div>
<div><font face="'courier new', monospace" size="4">from math import ceil</font></div><div><font face="'courier new', monospace" size="4"><br></font></div><div><font face="'courier new', monospace" size="4">def greedy(q):</font></div>
<div><font face="'courier new', monospace" size="4"> """return unit fraction expansion of fractions.Fraction q,</font></div><div><font face="'courier new', monospace" size="4"> using Fibonacci's 'greedy algorithm' -- non-recursive"""</font></div>
<div><font face="'courier new', monospace" size="4"><br></font></div><div><font face="'courier new', monospace" size="4"> results = [] </font></div><div><font face="'courier new', monospace" size="4"><br>
</font></div><div><font face="'courier new', monospace" size="4"> while q > 0:</font></div><div><font face="'courier new', monospace" size="4"> if q.numerator == 1:</font></div><div><font face="'courier new', monospace" size="4"> results.append(q)</font></div>
<div><font face="'courier new', monospace" size="4"> break</font></div><div><font face="'courier new', monospace" size="4"> x = Fraction(1,ceil(q.denominator / q.numerator))</font></div><div>
<font face="'courier new', monospace" size="4"> q = q - x</font></div><div><font face="'courier new', monospace" size="4"> results.append(x) </font></div><div><font face="'courier new', monospace" size="4"> return tuple(results)</font></div>
<div><font face="'courier new', monospace" size="4"><br></font></div><div><font face="'courier new', monospace" size="4">def _test( ):</font></div><div><font face="'courier new', monospace" size="4"> """</font></div>
<div><font face="'courier new', monospace" size="4"> >>> greedy(Fraction(5,121))</font></div><div><font face="'courier new', monospace" size="4"> (Fraction(1, 25), Fraction(1, 757), Fraction(1, 763309), Fraction(1, 873960180913), Fraction(1, 1527612795642093418846225))</font></div>
<div><font face="'courier new', monospace" size="4"> >>> greedy(Fraction(4,5))</font></div><div><font face="'courier new', monospace" size="4"> (Fraction(1, 2), Fraction(1, 4), Fraction(1, 20))</font></div>
<div><font face="'courier new', monospace" size="4"> >>> greedy(Fraction(9,31))</font></div><div><font face="'courier new', monospace" size="4"> (Fraction(1, 4), Fraction(1, 25), Fraction(1, 3100))</font></div>
<div><font face="'courier new', monospace" size="4"> >>> greedy(Fraction(21,50))</font></div><div><font face="'courier new', monospace" size="4"> (Fraction(1, 3), Fraction(1, 12), Fraction(1, 300))</font></div>
<div><font face="'courier new', monospace" size="4"> >>> greedy(Fraction(1023, 1024))</font></div><div><font face="'courier new', monospace" size="4"> (Fraction(1, 2), Fraction(1, 3), Fraction(1, 7), Fraction(1, 44), Fraction(1, 9462), Fraction(1, 373029888))</font></div>
<div><font face="'courier new', monospace" size="4"> """</font></div><div><font face="'courier new', monospace" size="4"> print("testing complete")</font></div><div><font face="'courier new', monospace" size="4"><br>
</font></div><div><font face="'courier new', monospace" size="4">if __name__ == "__main__":</font></div><div><font face="'courier new', monospace" size="4"> import doctest</font></div><div><font face="'courier new', monospace" size="4"> doctest.testmod() </font></div>
<div><font face="'courier new', monospace" size="4"> _test()</font></div><div><font face="'courier new', monospace" size="4"> </font></div></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">
<font face="'courier new', monospace" size="4"><span style="font-family: arial; font-size: small; "><div>Note that I'm calling these "pseudo Egyptian" -- not claiming there's any simple</div><div>
algorithmic solution that'll work best in all cases. Computer scientists and</div><div>Milo appear to be on the same side on this one.</div><div>"""</div></span></font></div></div><div><br></div><div>The threads on all this may be dredged up from an obscure Google </div>
<div>group named mathfuture, one of the Droujkova facilities, and as </div><div>usual productive.</div><div><br></div><div>Kirby</div><div><br></div>