Waste less time on Facebook — follow Brilliant.

Egyptian Fraction ALGORITHM !!

This is a programming challenge to all those avid programmers out there.

An Egyptian fraction is a fraction that can be expressed as a sum of two or more fractions, each with numerator 1. For example, 7/8 = 1/2 + 1/3 + 1/24 (notice all numerators are 1).There may be more than one possible answer. For example, 7/8 could also be equal to 1/2 + 1/4 + 1/8. You must output the one that maximises the first fraction output. Ties are broken by maximising the second fraction, then the third etc.

In the case of 7/8, the correct output should be 1/2 + 1/3 + 1/24


You will be given a numerator value "N" and a denominator value "D" and your programme must output the Egyptian fraction expansion.


Enter N: 31

Enter D: 47

Output: 1/2 + 1/7 + 1/60 + 1/19740


0 < D < N < 50

(Adapted from the South African Programming Olympiad 2002)

Note by Mark Mottian
3 years, 7 months ago

No vote yet
1 vote


Sort by:

Top Newest

Hi Mark! I did find an algorithm for it. Well I don't exactly know how efficient it is. But you can have a look at it. I tried it executing it in java and almost* got the answer. Here is how it is.

Divide the numerator by denominator.Express it as a decimal, say equal to d.

Invert the value of d and store it in some other variable say y. Using the ceil function on it to get the first denominator and store the integer value in say z.

Subtract the value of (1/z) from d.

Repeat the loop until y is an integer.( I checked whether y was an integer by checking whether the difference between the floor/ceil value of y and the value of y is zero. )

(Display the obtained values of z as you wish to within the loop before it gets changed.)

  • It did work pretty well but the problem was somehow it did not round up things well. For example my answer for 31/47 was coming out to be 1/2 + 1/7 + 1/60 + 1/19741 + another very small fractions. I checked it later and was mainly because it was displaying 31/47 - 1/2 - 1/7 - 1/60 as 19740.00000001523 which created that small error.
    Rest it did work out pretty well and I think that if you can help resolve that small approximation error it would work out to be a good method because this one of the most efficient algorithms i could think of.

If anyone can come up with a better way I would love to know that as well.

Sudeep Salgia - 3 years, 7 months ago

Log in to reply

Hi Sudeep! WOW! This algorithm has fantastic thinking!

I've implemented your algorithm into a Python program so that the other Python programmers on Brilliant can better understand your algorithm and try correct those minor approximation errors.

import math

a = 29      #let this be the numerator
b = 47      #let this be the denominator
y = 1.25    #set this initially to any non-integer value

d = (a + 0.0)/(b + 0.0) 

while math.floor(y) - math.ceil(y) <> 0:  
    y = 1.0/d   
    z = math.ceil(y)    
    d = d - 1/z
    print z,

Those approximation errors ruin everything!! Somebody PLEASE help correct this!!!

Thanks again Sudeep for your ingenuity! :)

Mark Mottian - 3 years, 7 months ago

Log in to reply

The python modules Decimal and Fraction might be useful here

Thaddeus Abiy - 3 years, 7 months ago

Log in to reply

You can create a function which subtracts the fractions the way we do manually, by taking lcm and stuff and the return the fraction as a ratio of two coprime numbers. I did try that and it worked out. I used an array for the purpose and if you want I can give you the code which I had executed in java.
It did solve the small error but somehow compromise on the efficiency a little bit in terms of computing values (which is pretty obvious, I believe).

Sudeep Salgia - 3 years, 7 months ago

Log in to reply

@Sudeep Salgia Please share your Java code.

Mark Mottian - 3 years, 7 months ago

Log in to reply

@Mark Mottian Here it is: Firstly the first term in each array is the numerator and second is the denominator.

Secondly I have included the main code corresponding to the algorithm and have omitted all those lines where I accept the values from the user etc.

public static int[ ] sub(int a[ ], int b[ ])    
//accept two arrays as parameters
{   // a has the given fraction(31/47) and b the obtained one (1/2)
      // r will store the result
    int r[] = new int[2];              
    r[0]= (a[0]*b[1])-(a[1]*b[0]);
    int t=gcd(r[0],r[1]);            
// gcd is my own function whose code is not included and returns
 the gcd of the passed values                                          
    r[0]/=t;r[1]/=t;          //make it a ratio of coprime numbers
    return r;

main( ) function:

double m;int z;int g[] =new int[2];g[0]=1;boolean b=true;
    int val[] = {n,d};    
// have already accepted N=n and D=d from the user
       //fraction stored as decimal
        z= (int)Math.ceil((1D/m));    // ceil value
        System.out.println(z);        //print ceil value
        val = sub(val,g);             
     //store the returned fraction again in the same variable so 
that we can reuse it and form a part of the loop

// if the numerator of the returned function is one then print the value and end the loop

Sudeep Salgia - 3 years, 7 months ago

Log in to reply

@Sudeep Salgia Very Nice! :)

Mark Mottian - 3 years, 7 months ago

Log in to reply

@Mark Mottian Thank you.

Sudeep Salgia - 3 years, 7 months ago

Log in to reply

do not use floating points when you're dealing with fractions

Chandler West - 3 years, 7 months ago

Log in to reply

47760---->the output 1/19740

Sunitha Bhadragiri - 3 years, 5 months ago

Log in to reply

I found this:


Tanishq Aggarwal - 3 years, 7 months ago

Log in to reply

If Python's Fraction makes this easy, Haskell's Ratio makes it trivial...

import Data.Ratio
import Data.List

showFrac f = show (numerator f) ++ "/" ++ show (denominator f)
showFracs = intercalate " + " . map showFrac

maxFracUnder f = 1 % ceiling (1 / f)

egyptian 0 = []
egyptian f = let m = maxFracUnder f in m : egyptian (f - m)

main = do
    cont <- getContents
    let [n, d] = map read (words cont) in putStrLn . showFracs . egyptian $ n % d

There are no sacrifices for speed, either; egyptian can compute the expansion of 539340989899 % 3112348903450 in under 0.6ms, without any optimization. (Note: Haskell uses % to denote ratios.)

Brian Chen - 3 years, 7 months ago

Log in to reply

Come on guys! Let's see what efficient algorithm you can come up with for this problem!!

(p.s. I really struggling to come up with something unique. PLEASE HELP!!)

Mark Mottian - 3 years, 7 months ago

Log in to reply

The fractions.Fraction abstraction in Python's stdlib is super useful for this problem.

Here is my find with one tricky line missing. How to efficiently move i?

from fractions import Fraction as F

def find(N, D):
    total = F(N, D)
    results = []
    i = 2
    while sum(results) != total:
        diff = total - sum(results)
        if diff.numerator == 1:
            return results

        ap = F(1, i)
        if sum(results) > total:

        # what to do with i here?? incrementing by 1 could take a lot of cycles ...
        i += 1

I somehow figured out a little trick about advancing i there at the end of the while iteration. I'll let you get the joy of figuring it out. This algo will run. But, it will not do every 0 < D < N < 50 in a reasonable amount of time. Try 5 / 31, for instance.

Skylar Saveland - 3 years, 7 months ago

Log in to reply

It's possible to find an efficient algorithm that can compute much larger Ns and Ds. I wrote a Python function, find. This function can calculate the fractions for N=539340989899 and D=3112348903450 in under 5ms.

In [1]: %timeit find(539340989899, 3112348903450)
100 loops, best of 3: 4.44 ms per loop

The denominators are given below:

In [2]: pprint.pprint([d.denominator for d in find(539340989899, 3112348903450)])



The sum of these fractions really does add up to \( \frac{539340989899}{3112348903450} \)

In [3]: print sum(find(539340989899, 3112348903450))
Fraction(539340989899, 3112348903450)

Skylar Saveland - 3 years, 7 months ago

Log in to reply

Hi Skylar! Thanks for replying! What programming language is this?

Mark Mottian - 3 years, 7 months ago

Log in to reply

Hi Mark :D It's Python. There is an interactive interpreter called IPython that makes debugging and experimenting fun and easy.

Skylar Saveland - 3 years, 7 months ago

Log in to reply

@Skylar Saveland No way! I've never seen Python programming written like that! What are all those long scary numbers?

Mark Mottian - 3 years, 7 months ago

Log in to reply

@Mark Mottian I added some explanation. I wrote the find function. The implementation is not shown. The output is just me playing with the function in the interactive interpreter.

Skylar Saveland - 3 years, 7 months ago

Log in to reply

@Skylar Saveland Please share your find function with me. "This function can calculate the fractions for N=539340989899 and D=3112348903450 in under 5ms." Now that's efficient! :)

Mark Mottian - 3 years, 7 months ago

Log in to reply


Problem Loading...

Note Loading...

Set Loading...