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
TASK
You will be given a numerator value "N" and a denominator value "D" and your programme must output the Egyptian fraction expansion.
Example:
Enter N: 31
Enter D: 47
Output: 1/2 + 1/7 + 1/60 + 1/19740
Constraints:
0 < D < N < 50
(Adapted from the South African Programming Olympiad 2002)
Easy Math Editor
This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.
When posting on Brilliant:
*italics*
or_italics_
**bold**
or__bold__
paragraph 1
paragraph 2
[example link](https://brilliant.org)
> This is a quote
\(
...\)
or\[
...\]
to ensure proper formatting.2 \times 3
2^{34}
a_{i-1}
\frac{2}{3}
\sqrt{2}
\sum_{i=1}^3
\sin \theta
\boxed{123}
Comments
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.)
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.
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.
Those approximation errors ruin everything!! Somebody PLEASE help correct this!!!
Thanks again Sudeep for your ingenuity! :)
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).
Log in to reply
Log in to reply
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.
main( ) function:
Log in to reply
Log in to reply
The python modules Decimal and Fraction might be useful here
do not use floating points when you're dealing with fractions
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.
The denominators are given below:
The sum of these fractions really does add up to 3112348903450539340989899
Log in to reply
Hi Skylar! Thanks for replying! What programming language is this?
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.
Log in to reply
Log in to reply
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!!)
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?
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.
If Python's Fraction makes this easy, Haskell's Ratio makes it trivial...
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.)I found this:
http://en.wikipedia.org/wiki/GreedyalgorithmforEgyptianfractions
47760---->the output 1/19740