[Edu-sig] Brute force solutions
urnerk@qwest.net
urnerk at qwest.net
Sun Sep 25 00:03:27 CEST 2005
>The fun part here is we can use numerator/denominator syntax with
open-ended
>precision integers, to like express sqrt of 19 as some humongous fraction
>(as many digits as memory will allow). This lets us far surpass the
>floating point barrier.
For example, the sqrt of 19 is rougly:
745969084203762918506900171
6630770611758990188906918072
4013315460866431392624885605
5112125808098871177881508095
4010864598095
----------------------------
1711370448973571545640915466
3001505416992487326376156603
4301985589644920244770721090
4993017822174818974832607428
966608330904
(that's a numerator over a denominator).
Here's the code behind it (still need to replace int(pow(x,0.5))
with a numerical method that doesn't do all the work to find
an actual floating point sqrt -- overkill):
"""
gonzo.py
"""
from mathteach import cf2
# http://www.4dsolutions.net/ocn/python/mathteach.py
def sqrtcf(n):
orig = n
denom = 1
addterm = 0
cflist = []
while True:
m = int(pow(n,0.5)) # <-- replace with function call
cflist.append(m)
if len(cflist)>1 and denom == 1:
break
addterm = -(addterm - m*denom)
denom = (orig - addterm**2)/denom
n = ((pow(orig,0.5) + addterm)/denom)**2
return cflist
def getsqrt(n,default=30):
thelist = sqrtcf(n)
while len(thelist)<default:
thelist += thelist[1:]
thelist = thelist[:default]
return cf2(thelist * 10)
>>> from gonzo import getsqrt
>>> getsqrt(19)
...
Kirby
_______________________
Edu-sig mailing list
Edu-sig at python.org
http://mail.python.org/mailman/listinfo/edu-sig
--------------------------------------------------------------------
mail2web - Check your email from the web at
http://mail2web.com/ .
More information about the Edu-sig
mailing list