Find the unique infinite regular continued fraction for any given irrational number with python

I noticed the following description here:

The numerical value of an infinite continued fraction is [irrational](https://en.wikipedia.org/wiki/Irrational_number); it is defined from its infinite sequence of integers as the [limit](https://en.wikipedia.org/wiki/Limit_(mathematics)) of a sequence of values for finite continued fractions. Each finite continued fraction of the sequence is obtained by using a finite [prefix](https://en.wikipedia.org/wiki/Prefix_(computer_science)) of the infinite continued fraction's defining sequence of integers. Moreover, every irrational number α {\displaystyle \alpha } ![\alpha ](https://wikimedia.org/api/rest_v1/media/math/render/svg/b79333175c8b3f0840bfb4ec41b8072c83ea88d3) is the value of a *unique* infinite regular continued fraction, whose coefficients can be found using the non-terminating version of the Euclidean algorithm applied to the [incommensurable](https://en.wikipedia.org/wiki/Commensurability_(mathematics)) values α {\displaystyle \alpha } ![\alpha ](https://wikimedia.org/api/rest_v1/media/math/render/svg/b79333175c8b3f0840bfb4ec41b8072c83ea88d3) and 1. This way of expressing real numbers (rational and irrational) is called their *continued fraction representation* .

And I also noticed the following tutorial here: Representing Rational Numbers With Python Fractions.

So I want to know if it’s possible to find the unique infinite regular continued fraction for any given irrational number with python.

Regards,
HZ

Off the top of my head, I’m sure you can, since this is not a language problem but rather a mathematical problem.

The problem is, how do you input the irrational number to the function? Not all of them have easy representations like sqrt(2). What about pi or e? How do you define a system to input any irrational number?

That’s in fact infeasible. The number of possible programs in any language is countable. On the other hand, the set of irrational numbers is non-countable. Therefore, whatever the way you would define a system to represent irrational numbers in any programming language (not just Python), it would not be able to represent all irrational numbers.

So the problem really depends on the specific form that your input irrational numbers have.

1 Like

Hongyi Zhao asked:

"So I want to know if it’s possible to find the unique infinite regular

continued fraction for any given irrational number with python."

Does your computer have an infinite amount of memory? Are you prepared

to spend an infinite amount of time calculating the sequence of partial

continued fractions?

If the answer to either of those questions is No, then no, there will be

irrational numbers you cannot compute the continued fraction for.

If your answer is Yes to both of those questions, then the answer is

still no, because most irrational numbers are non-computable.

Here is an example of a real number (actually an infinitely large family

of real numbers) which are non-computable:

So there are an infinite number of irrational numbers which can never be

computed.

Okay, but what about the computable irrational numbers, like π (pi), e,

square root of 2 etc? The answer is still no, for most of them you

cannot compute their continued function exactly. There are some

exceptions, if you have some way of representing a repeating continued

fraction, you can represent some irrationals exactly:

 √3 = [1;1,2,1,2,1,2,1,2,...]

but in general, most irrational numbers are infinite but not repeating

continued fractions.

So unless you have infinite time and infinite memory, the best you can

do is compute some finite approximation.

1 Like