[Edu-sig] Long Integer Fractions

Kirby Urner pdx4d@teleport.com
Fri, 12 May 2000 23:58:05 -0700


>From the same web page source, this one converges
>faster than Halley's looks like.

... except if1() seems to spiral out of control for P > 100=20
or so.  Hmmmmm...  Halley's more stable.  Maybe I'm doing=20
something wrong.

I think it's SmallTalk that does integer fractions, i.e.=20
allows calcs with numerators and denominators staying=20
separate.  Does Scheme do this too?  I forget just now
(I practiced with it for awhile, but am still focussed
on a math curriculum with Python the main computer=20
language).

Anyway, I wrote a Fraction class that sort of does=20
this (computes with fractions) -- maybe I'm reinventing=20
a wheel or two here.

Fraction in action:

 >>> from roots import Fraction
 >>> a =3D Fraction(1,6)  # i.e. 1/6
 >>> b =3D Fraction(2,3)  # i.e. 2/3
 >>> c =3D a+b  # add fractions
 >>> c.str()
 '5L/6L'
 >>> c =3D a-b  # subtract fractions
 >>> c.str()
 '-3L/6L'
 >>> c.simplify()
 >>> c.str()
 '-1L/2L'     =20
 >>> c=3D(a*b).str()  # multiply factions (divide OK too)
 >>> c.str() =20
 '2L/18L'
 >>> c.simplify()
 >>> c.str()
 '1L/9L'

I used my primes.py module for its gcd and lcm functions=20
(greatest common divisor, lowest common multiple).  One=20
could make 'simplify' more "built-in" upon initialization
(making a separate call unnecessary), but I'm still=20
working through Domingo G=F3mez Mor=EDn's web page and didn't=20
want to simplify automatically.

Using my Fraction class and one of Domingo's algorithms,=20
I was able to get some impressive long integer fractions=20
for 3rd root of 2, 3rd root of 10:

3rd root of 2 is approx =3D

   65379522
   ---------
   51891761

3rd root of 10 is approx =3D

   91969780593702397138462508494860
   ---------------------------------
   42688590663356403236303435376201

Kirby