I thought up yet another algorithm for egyptian fraction expansion which turned out to be very effective (in terms of the length and the denominator size) - in most cases. However, for some fractions it doesn't terminate at all - it leads to an infinite loop.

Here is the algorithm:

Let $\frac{p}{q}<1$ and $p,q$ coprime. Find the minimal $m$ such that $q/(p+m)$ is an integer. We only need to consider $m \in [1,q-p]$. Represent the fraction as: $$\frac{p}{q}=\frac{p+m}{q}-\frac{m}{q}$$

Now to obtain a positive term instead of a negative one, we split the first fraction in two: $$\frac{p+m}{q}-\frac{m}{q}=\frac{p+m}{2q}+\frac{p+m}{2q}-\frac{m}{q}=\frac{p+m}{2q}+\frac{p-m}{2q}$$

Here is a conditional: if $p<m$, then repeat the previous step (add $\frac{p+m}{4q}$ to the negative fraction). And so on, until both terms are positive. If $p>m$ then $\frac{p}{q} \to \frac{p-m}{2q}$ and we repeat the first step of the algorithm.

The working name is complementary method, so I will use CM to denote it for now.

Despite its simplicity (it's not at all obvious why we are dividing by $2$ instead of using some other way to expand the first term) the algorithm works very well. In a lot of cases it beats every other algorithm I tried.

Since the greedy algorithm and Engel expansion are usually bad in terms of denominator size, I used two other methods to compare: Binary Remainder Method and my own 'Splitting-Joining method' (the details can be found in my Mathematica SE question. I also compared it to a modification of Engel proposed by Daniel Fischer in this answer and CM is mostly better for the examples he provided.

Some examples of the best results (a sequence of denominators is provided in each case):

4/49: CM {14,98}; BR {16,98,196,392,784}; SJ {13,325,925,1813}

3/35: CM {14,70}; BR {16,70,140,560}; SJ {20,28}

47/104: CM {4,8,13}; BR {4,8,16,104,208}; SJ {4, 14, 26, 28, 52, 70, 104, 130, 182}

Some examples of the worst results (but still valid - algorithm terminates):

94/191: CM {4, 8, 16, 32, 64, 256, 512, 1024, 2048, 4096, 8192, 24448, 48896, 97792, 195584, 391168, 782336, 1564672}

65/157: CM {4, 8, 32, 256, 512, 1024, 2048, 4096, 10048, 20096, 40192, 80384, 160768, 643072}

52/139: CM {4, 16, 32, 64, 128, 278, 556, 1112, 2224, 8896, 17792}

However, in these cases both BR and SJ methods also give long expansions with large denominators.

Now the real problem which I'm trying to solve - why in some cases the algorithm doesn't terminate, but leads to loops? From large scale experiments I estimates the proportion of such fractions to be about $1.8$% (for numerators and denominators below $1000$).

The examples of such 'bad' fractions are:


The most common denominator is $87$ for some reason. Note that not all of the denominators and/or numerators are prime.

The problem can be solved by using $\frac{p-1}{q}$ instead, but not in every case, for example $7/87$ doesn't work either. However, both $6/87$ and $2/87$ work, and give different denominators, so we can expand $8/87$ after all.

I think the problem might be related to the use of the expansion $1=1/2+1/2$ to divide the fist term. However, when I tried some other schemes, I didn't get good results (for example, I've got repeating fractions when using $1=1/3+2/3$).

The working Mathematica code for the algorithm is:

a[[j]]=(p0+M)/(2 q0);

And the second question: how to modify the algorithm so it always terminates?


Among the first $10000$ fractions with $p \neq 1$ in lexicographic order there are $269$ fractions for which this algorithm doesn't work. (Seems to be more than $2$%). They are:


Update 2 (Important)

The question was deleted for a time, because for some of the listed fraction algorithm seems to work just fine if it's done by hand. There is some error in my code, which I hadn't been able to find yet.

But there are fractions which lead to loops by hand as well (such as $41/111$) so the question still stands.

  • 1
    $\begingroup$ This could be entirely meaningless, but I notice that every entry of oeis.org/A273199 (up to $183$) appears as a denominator in your list of $269$ "bad" fractions. (I found the entry by searching on $57,69,87,93$, which are the first few denominators in your list that appear with many numerators.) $\endgroup$ – Barry Cipra Apr 5 '17 at 12:01

There's still an error in your code, in the While loop you take if $p<m$:


The problem is that after you update p1, the calculation q1 = Denominator[a[[j]] + p1/q1] is done with the old value of q1 but the new value of p1. A quick fix: change the loop to

{p1,q1}={Numerator[a[[j]]+p1/q1], Denominator[a[[j]]+p1/q1]}];

Here's my own implementation of this algorithm, in convenient function-call form: just run egyptian[41/111] to get back {1/4,1/12,1/48,1/74,1/592}. I've tested it on all fractions with denominator less than $200$, and it works correctly:

split[Rational[p_,q_]]:=Module[{m, t=1},
  While[(2^t-1)p < m,t++];
  Return[{(p+m)/(2^t q),((2^t-1)p-m)/(2^t q)}];

  {first,rest} = split[fraction];
  Return[Prepend[first] @ egyptian[rest]];

(Here, split is a helper function that returns the two parts of the representation of $\frac pq$ as $\frac{p+m}{2q} + \frac{p-m}{2q}$ or as $\frac{p+m}{4q} + \frac{3p-m}{4q}$ or in general as $\frac{p+m}{2^t q} + \frac{(2^t-1)p-m}{2^t q}$ for whichever $t$ is the first to work. Then egyptian just repeatedly applies this to get the Egyptian fraction we wanted.)

  • $\begingroup$ Thanks! I'm going to check the fractions I had a problem with by hand (don't remember where I put my notes on this), and thank you very much for finding the mistake in my algorithm! Going to test your corrections on a larger number of fractions, there is likely an improvement even in cases which worked before $\endgroup$ – Yuriy S Apr 5 '17 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.