0

I am going to implement a Farey fraction approximation for converting limited-precision user input into possibly-repeating rationals.
http://mathworld.wolfram.com/FareySequence.html

I can easily locate the closest Farey fraction in a sequence, and I can find Fn by recursively searching for mediant fractions by building the Stern-Brocot tree.
http://mathworld.wolfram.com/Stern-BrocotTree.html

However, the method I've come up with for finding the fractions in the sequence Fn seems very inefficient:
(pseudo)

For int i = 0 to fractions.count -2
{
    if fractions[i].denominator + fractions[i+1].denominator < n
    {    
        insert new fraction(
            numerator = fractions[i].numerator + fractions[i+1].numerator
            ,denominator = fractions[i].denominator + fractions[i+1].denominator)
            //note that fraction will reduce itself
        addedAnElement = true
    }
}
if addedAnElement 
    repeat

I will almost always be defining the sequence Fn where n = 10^m where m >1

So perhaps it might be best to build the sequence one time and cache it... but it still seems like there should be a better way to derive it.

EDIT:
This paper has a promising algorithm:
http://www.math.harvard.edu/~corina/publications/farey.pdf

I will try to implement.
The trouble is that their "most efficient" algorithm requires knowing the prior two elements. I know element one of any sequence is 1/n but finding the second element seems a challenge...

EDIT2:
I'm not sure how I overlooked this:
Given F0 = 1/n
If x > 2 then
F1 = 1/(n-1)

Therefore for all n > 2, the first two fractions will always be
1/n, 1/(n-1) and I can implement the solution from Patrascu.

So now, we the answer to this question should prove that this solution is or isn't optimal using benchmarks..

tshepang
  • 12,111
  • 21
  • 91
  • 136
Matthew
  • 10,244
  • 5
  • 49
  • 104

2 Answers2

1

Why do you need the Farey series at all? Using continued fractions would give you the same approximation online without precalculating the series.

Vlad
  • 35,022
  • 6
  • 77
  • 199
  • Can you show me an implementation for a rational approximation using this method? – Matthew Nov 14 '11 at 19:09
  • Well, I don't have an _implementation_ of that. But it can be easily done like this: (1) you calculate the continuous fraction for your input number (described [here](http://en.wikipedia.org/wiki/Continued_fraction#Calculating_continued_fraction_representations), for rational input the algorithm is finite); (2) you truncate the continuous fraction ([here](http://en.wikipedia.org/wiki/Continued_fraction#Best_rational_approximations) are more details and algorithm: not every truncation is appropriate), and expand it into a rational number. – Vlad Nov 14 '11 at 19:38
  • Or you may interpret user input as an interval, and look at the implementation in the [section below](http://en.wikipedia.org/wiki/Continued_fraction#Best_rational_within_an_interval). The algorithm is described there is detail. – Vlad Nov 14 '11 at 19:39
  • I'm sure this will ultimately give me a complete fraction set for any rational but I seem to have some trouble using it for when I want a rational *estimate* of a potentially repeating value. For example, I don't seem to get the result I'm looking for when trying to get 2/3 for .6666. I accept that if I repeated the process *enough times* that I would reach a result... but I hesitate to commit that it's faster/easier than a pre-calculated Farey sequence. – Matthew Nov 14 '11 at 22:11
  • 1
    For your case, everything must be quite simple. Your fraction is 0.6666 = 1/(1+(1/(1+(1/(1+1/1666))))) which is easily approximated by 1/(1+(1/(1+(1/1)))), which is exactly 2/3. The next approximation would be 1/(1+(1/1)) = 1/2, the next is 1/1. – Vlad Nov 14 '11 at 22:23
  • excellent point, I had misunderstood adding the continued fraction ints. – Matthew Nov 14 '11 at 22:28
  • @Matthew: hope my idea helps. – Vlad Nov 14 '11 at 22:33
  • +1 because it helps me in other ways... but I don't think it actually answers the question. – Matthew Nov 14 '11 at 22:39
  • @Matthew: well, the question was anyway about Farey sequence :-) – Vlad Nov 14 '11 at 22:40
0

Neighboring fractions in Farey sequences are described in Sec. 3 of Neighboring Fractions in Farey Subsequences, http://arxiv.org/abs/0801.1981 .

Rory
  • 21
  • 2
  • Determining neighbors is resolved in the paper I posted. This question is regarding the efficiency of the method for determining the entire set. The math for determining the set is trivial, I'm interested in the efficiency of the method. Good link though! – Matthew Nov 07 '11 at 16:02