zIFBoards - Free Forum Hosting
Free Forums. Reliable service with over 8 years of experience.

Learn More · Register Now
Welcome to Dozensonline. We hope you enjoy your visit.
You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, and sending personal messages. Registration is simple, fast, and completely free. (You will be asked to confirm your email address before we sign you on.)
Join our community!
If you're already a member please log in to your account to access all of our features:

Name:   Password:


 

 Python Code For Continued Fractions
Dan
Posted: Apr 4 2017, 04:26 AM


Dozens Disciple


Group: Members
Posts: 1,463
Member No.: 19
Joined: 8-August 05



Here are functions for converting rational numbers from the continued fraction representation [a, b, c, d, ...] to the Fraction object a+1/(b + 1/(c + 1/(d + ...))) that it represents.

CODE
from fractions import Fraction

def contfrac_to_frac(cfr):
   if len(cfr) == 0:
       return Fraction(0)
   elif len(cfr) == 1:
       return Fraction(cfr[0])
   else:
       return Fraction(cfr[0]) + 1 / contfrac_to_frac(cfr[1:])

def frac_to_contfrac(frac):
   frac = Fraction(frac)
   if frac.denominator == 1:
       return [frac.numerator]
   else:
       ipart, fpart = divmod(frac, 1)
       return [int(ipart)] + frac_to_contfrac(1 / fpart)


I realize that recursion isn't the most efficient way of doing things (and throws an exception if you hit the 1000 stack frame limit), but it's simple. I may post an "optimized" version later.

Note that since all finite float values are rational, they will all have a finite continued fraction representation.

Some interesting "irrational" values are:

CODE
>>> frac_to_contfrac(math.e)
[2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 12, 1, 1, 11, 1, 1, 1, 11, 5, 1, 1, 2, 1, 4, 2, 1, 1, 9, 17, 3]
>>> frac_to_contfrac(math.sqrt(2))
[1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 7, 1, 2, 33, 2, 7, 5, 2, 1, 1, 16, 2]
>>> frac_to_contfrac((1 + math.sqrt(5)) / 2)
[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, 1, 1, 1, 1, 1, 1, 2, 7, 1, 5, 5, 1, 1, 2, 4, 1, 1, 10, 1, 2, 1, 8]
Top
icarus
Posted: Apr 4 2017, 09:17 PM


Dozens Demigod


Group: Admin
Posts: 1,913
Member No.: 50
Joined: 11-April 06



Mathematica has the ContinuedFraction command and does the same thing. For instance OEIS A003417 = e is generated by

CODE
Block[{$MaxExtraPrecision = 1728},
ContinuedFraction[E, $MaxExtraPrecision]]


This gives the first great gross of terms. (We could just use ContinuedFraction[E, 120] if we just wanted two dozen handfuls of terms.)

We could also write a "cheat" for it:

CODE
Array[{Boole[# == 1] + 1, 1, 2 #} &, 12] // Flatten


This gives the first 3 dozen terms, recognizing it is 2, 1, 2, then repeats 1, 1, and 2(n/3) for every digit in a position that is congruent to 0 (mod 3).

Unfortunately Mathematica (Wolfram) is really my only language that I use nowadays (unfortunate for it is proprietary and is not a widely known language because of it). At least Python has sympy and other packages that help it along mathwise. There is also PARI and Matlab.
Top
jrus
Posted: Apr 19 2017, 10:16 PM


Regular


Group: Members
Posts: 195
Member No.: 1,156
Joined: 23-October 15



Top
Ruthe
Posted: Apr 26 2017, 08:28 PM


Regular


Group: Members
Posts: 360
Member No.: 47
Joined: 27-February 06



QUOTE (Dan @ Apr 4 2017, 04:26 AM)
Here are functions for converting rational numbers from the continued fraction representation [a, b, c, d, ...] to the Fraction object a+1/(b + 1/(c + 1/(d + ...))) that it represents.


Romans had no way to represent most fractional values but relied on listing a value as the sum of ever smaller fractions up to a point they felt was of sufficient accuracy. These fractions were invariably sub-multiples of twelfths and eighths, written as the names of each fraction down to the smallest used by the Romans which was 1/2304 which is 1/(2^4 x 12^2) = approx 0.000434027 (Added this: or 0.0009 uncial)

PPS The Romans used mainly twelfths for fractional values but also saw the usefullness of eighths, so had names for halves, quarters, eighths and sixteenths and their combinations with sub multiples of twelfths. They saw that 1/8 was just 1 twelfths.
Top
« Next Oldest | General | Next Newest »
DealsFor.me - The best sales, coupons, and discounts for you

Topic Options



Hosted for free by zIFBoards* (Terms of Use: Updated 2/10/2010) | Powered by Invision Power Board v1.3 Final © 2003 IPS, Inc.
Page creation time: 0.0230 seconds · Archive