[Edu-sig] Algebra 2
kirby urner
kirby.urner at gmail.com
Tue Oct 7 06:45:58 CEST 2008
>
> Continued fractions do, especially:
>
> IDLE 1.2.1
> >>> from __future__ import division
> >>> 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1))))))
> 0.61904761904761907
> >>> 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1))))))
> 0.61538461538461542
> >>> 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 +
> 1)))))))))
> 0.6179775280898876
>
Just to extend here a bit, these generators are not computationally
efficient; there's a much better way to do continued fractions, in terms of
algorithms, Tim Peters showing me some ropes early on in this archive,
Cut-the-Knot a pretty good website as I recall, plus several others...
However, sometimes computational efficiency isn't the point so much as
conceptual transparency, and in this respect I think these two have
pedagogical value:
In Python 3.x...
IDLE 3.0a2
>>> from continued import *
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'continued_1',
'continued_2']
>>> thegen = continued_2() # less pathological
>>> for i in range(10): print ( next ( thegen) )
1/(1 + 0)=1.0
1/(1 + 1/(1 + 0))=0.5
1/(1 + 1/(1 + 1/(1 + 0)))=0.666666666667
1/(1 + 1/(1 + 1/(1 + 1/(1 + 0))))=0.6
1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 0)))))=0.625
1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 0))))))=0.615384615385
1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 0)))))))=0.619047619048
1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 +
0))))))))=0.617647058824
1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 +
0)))))))))=0.618181818182
1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 +
0))))))))))=0.61797752809
>>> thegen = continued_1() # the first thing I tried
>>> for i in range(10): print ( next ( thegen) )
1/(1 + 0)=1.0
1/(1 + 1/(1 + 0))=0.5
1/(1 + 1/(1 + 1/(1 + 1/(1 + 0))))=0.6
1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 +
0))))))))=0.617647058824
1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 +
1/(1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + 0))))))))))))))))=0.6180338134
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
for i in range(10): print ( next ( thegen) )
File "/home/kirby/continued.py", line 4, in continued_1
yield expr % 0 + "=" + str (eval (expr % "0") )
MemoryError
>>>
def continued_1( ):
expr = "1/(1 + %s)"
while True:
yield expr % 0 + "=" + str (eval (expr % "0") )
expr = expr % expr # doubles in size, overflow quickly
def continued_2( ):
expr = "1/(1 + %s)"
while True:
yield expr % 0 + "=" + str (eval (expr % "0") )
expr = expr % "1/(1 + %s)" # more incremental, converges more slowly
Kirby
Princeton '80
(noticing krstic's harvard.edu)
PS: for those not up on this abstruse topic, we're approaching 1/phi in the
above examples where phi == (1 + math.sqrt( 5 ))/2
>
> [ much easier when you have color-coded parentheses checking, like in IDLE
> ]
>
> Better in Akbar font. We appear to be approaching something, from above
> and below.
>
> Kirby
>
>
>
>>
>>
>> --
>> Ivan Krstić <krstic at solarsail.hcs.harvard.edu> | http://radian.org
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20081006/bf26cf11/attachment-0001.htm>
More information about the Edu-sig
mailing list