If I want to obtain the best approximate fraction/rational for a given real number and the specificied maximum denominator as an integer, how to do this in mathematica? Many thanks.

Look at Help for `Rationalize`

. `RootApproximant`

can be also useful

Convergents of continued fractions offer a useful method for getting better and better fractional representations of an irrational number. I've also found them helpful for understanding connections to other ideas by way of the Euclidean algorithm.

Let's use convergents to approximate pi and the square root of two.

```
ClearAll[approximate];
approximate[r_, nConvergents_: 8, precision_: 10] :=
With[{c = Convergents[ContinuedFraction[r, nConvergents]]},
TableForm[Transpose[{c, N[r - c, precision]}],
TableHeadings -> {None, {Row[{"approximation of ", r}], "error"}}]]
```

Here's are the first 8 convergents for pi:

```
approximate[Pi]
```

Here are the first 8 convergents for `Sqrt[2]`

:

```
approximate[Sqrt[2]]
```

The successive error terms shrink and alternate direction as convergence advances.

In `approximate`

, you can optionally specify the number of convergents and precision desired.

Enjoy.

Here's some additional documentation about continued fractions, including some lovely demonstrations.

Similar Questions

I have a web application in which I render one or more Crystal Reports and then merge them into one PDF. What I'd like to do is obtain the number of pages from one CrystalDecisions.CrystalReports.Engi

I have a question about NDSolve function in Mathematica. I have an oscillator defined by these two equations: x' = v v' = -x - u*v^3 where u is some constant. How to create an NDSolve that resolves t

I want to write fraction value such as the picture below: How do I write fraction value using html without using image? NOTE: I don't want this 1 1/2 pattern but strictly just as the pic above

I am looking for the best way to convert a Number to a BigDecimal. Is this good enough? Number number; BigDecimal big = new BigDecimal(number.toString()); Can we lose precision with the toString() me

I understand neural networks with any number of hidden layers can approximate nonlinear functions, however, can it approximate: f(x) = x^2 I can't think of how it could. It seems like a very obvious

When plotting a function using Plot, I would like to obtain the set of data points plotted by the Plot command. For instance, how can I obtain the list of points {t,f} Plot uses in the following simpl

I would like to know the steps Mathematica applies to obtain a result. I have Limit[n^Log[2, n]/1.001^n, n -> \[Infinity]] and Mathematica shows 0 but I want to know how it got that result.

I would like to plot a simple interval on the number line in Mathematica. How do I do this?

How can i convert a binary fraction or a decimal fraction into hex in Java? What is the algorithm to do that? for example i want (11.110)2 be converted into (3.C)16 and which data type do I need to h

I want to implement simple classification tree (binary classification) using Mathematica. How can I implement a binary tree in Mathematica? Is there is a symbol for doing that?

I've a decimal value 34.3287332 how can i get the fraction of the value like .3287332 please any one help (I can convert that to string and get the fraction. but i dont need to)

How would you render a map such as this one within Mathematica? (e.g. one with US states and Canadian provinces colourable separately?) http://upload.wikimedia.org/wikipedia/commons/archive/1/18/20071

How to properly enable syntax highlighting for Mathematica in Vim? I want it automatically on when opening .m files with Vim too.

I already know when a fraction is repeating decimals. Here is the function. public bool IsRepeatingDecimal { get { if (Numerator % Denominator == 0) return false; var primes = MathAlgorithms.Primes(De

I have a string with hard drive volume information (the message is displayed in a message box) Volume In Drive E is NEW VOLUME Volume Serial Number is 9AE4-F468 I want to read only 9AE4F468 witho

I am trying to plot slope fields of some differential equations using mathematica but can't figure it out. Say I have the equation y' = y(t) y(t) = C * E^t How do I plot the slope field? I found an

Lets say there are N numbers grouped into K disjoint sets. The problem is to create a key for each of these disjoint sets such that given any number, a simple operation on these keys and the number sh

I need to store dates such as 'Summer 1878' or 'Early June 1923', or even 'Mid-afternoon on a Tuesday in August'. How would you suggest I do this? I have considered breaking the date and time up into

How to obtain a current page size of the memory MS Windows 7 in C#? In some cases we need it to allocate memory in best way. Thank you! UPDATES: Here is a sample code... I have some doubts here byte[]

I'm struggling to figure out how I can obtain an image from a UIImagePickerControllerReferenceURL (I know that I can obtain it directly from UIImagePickerControllerOriginalImage however, I have reason

I am trying to prepare the best tools for efficient Data Analysis in Mathematica. I have a approximately 300 Columns & 100 000 Rows. What would be the best tricks to : Remove, Extract or simpl

I am thinking of process an image to generate in Mathematica given its powerful image processing capabilities. Could anyone give some idea as to how to do this? Thanks a lot.

All, I'm really stuck trying to understand the best way to stream real time output of ffmpeg to a HTML5 client using node.js, as there are a number of variables at play and I don't have a lot of exper

With success I have written the following code, based on a for loop to approximate the number pi using the Monte-Carlo-method: function piapprox = calcPiMC(n) count = 0; % count variable, start value

What is the best way to define a numerical constant in Mathematica? For example, say I want g to be the approximate acceleration due to gravity on the surface of the Earth. I give it a numerical value

Let us suppose that we have a string representing a binary fraction such as: .1 As a decimal number this is 0.5. Is there a standard way in Python to go from such strings to a number type (whether

Is there a way to obtain the number of token in a string obtained by Method Scanner in Java? I mean, i can use s = sc.nextLine() to obtain an input line as a string. But on this string I cannot use le

I refer to this post http://r.789695.n4.nabble.com/Questions-about-biglm-td878929.html which discusses on how to obtain VIF using biglm. Is there an alternative way of obtaining VIF from the object p

Is there a library that will convert a Double to a String with the whole number, followed by a fraction? For example 1.125 = 1 1/8 I am only looking for fractions to a 64th of an inch.

I am trying to get Mathematica to approximate an integral that is a function of various parameters. I don't need it to be extremely precise -- the answer will be a fraction, and 5 digits would be nice

I am wondering what the best way to obtain the current domain is in ASP.NET? For instance: http://www.domainname.com/subdir/ should yield http://www.domainname.com http://www.sub.domainname.com/subdir

Can Mathematica do Bayes Rule conditional probability calculations, without doing the calculation manually? If so how? I have been searching both the Mathemtaica doco and the web for a hint but cannot

How can this Mathematica code be ported to Python? I do not know the Mathematica syntax and am having a hard time understanding how this is described in a more traditional language. Source (pg 5): ht

I was wondering which is the best machine learning technique to approximate a function that takes a 32-bit number and returns another 32-bit number, from a set of observations. Thanks!

I'm using scipy's fmin_l_bfgs_b optimization method on a 2-dimensional function available as a black box. Gradients cannot be evaluated directly, so I'm asking the method to approximate the gradients

this is an extension to my previous post, here is the updated code, so far it will ouput the last number I inputed into the array, so it's not actually summing values yet ;****************************

Suppose I have a real number. I want to approximate it with something of the form a+sqrt(b) for integers a and b. But I don't know the values of a and b. Of course I would prefer to get a good approxi

How does Mathematica create an InterpolatingFunction object? Example: test1 = FunctionInterpolation[Sin[x],{x,0,2*Pi}] The FullForm of test1 is long, but is primarily x values with the corresponding

This is an extension to the question here. Now I quote CommonsWare There is no reliable way to retrieve mobile number of device programatically. The only option remaining and I can think of is getti

Is there any possibility to properly set the fraction of a second, not milliseconds. I tried to parse the date 2013.06.08 12:46:44.41234 using the pattern yyyy.MM.dd H:mm:ss.SSSSS and the output was:

How does Mathematica decide when to round numbers in its output? For example, giving the input 250000.5 gives the output 2500001 While 25000.5 is indeed printed as 25000.5 N[] isn't helpful here

Given the account and password, how to obtain the contact list of that account in msn? Are there any msn api can do this? Or I have to use the account and password to login and obtain the contact list

How to obtain the following surface via Matplotlib? It is easy in matlab via: mesh(peaks) It seems matplotlib does not have an exact counterpart of mesh in matlab. the Wireframe plots does not have a

I am formatting floating point numbers and right now I have the %0.2f formatter, but I'd like to omit the .00 if the floating point number is an even integer. Of course I can think of string replacing

How do you put additional information into a Mathematica package file, like parameter specification doctests/example usage? Up to now, I only found the ::usage string and some information on how to

First of all, thank you for all your responces. I am doing my best to keep up with all the suggestions. However I'm trying to stay on track. So now I have s = Table[RandomChoice[{Heads, Tails}, 2 i +

Is there any relationship to convert a [r,g,b] tuple to a real number x, which is mapped on the the colorscale 0 to 1. Something like x = b + n*g + n^2*r. For example, this relationship will give [1,0

I have some embarrassingly-parallelizable work in a .NET 3.5 console app and I want to take advantage of hyperthreading and multi-core processors. How do I pick the best number of worker threads to ut

I,m looking for some method that can let me obtain (in windows) the directory where windows is saved (for example in my PC it will return C:\windows. I need it because I have to call this method pub

I'm working with the Excel format where 0.6549 Means that we are 65.49% through the day. How would I convert this to a nice looking time such as 15:30:42 PM in Java?