From 4e3d58f630965a52caa411208c6d28f63ac67440 Mon Sep 17 00:00:00 2001 From: Satyam Bharti <44036099+satyambharti171@users.noreply.github.com> Date: Thu, 31 Oct 2019 15:50:19 +0530 Subject: [PATCH 1/3] Create readme.md --- algorithms/Maths/Egyptian_Fractions/readme.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 algorithms/Maths/Egyptian_Fractions/readme.md diff --git a/algorithms/Maths/Egyptian_Fractions/readme.md b/algorithms/Maths/Egyptian_Fractions/readme.md new file mode 100644 index 0000000..eb25766 --- /dev/null +++ b/algorithms/Maths/Egyptian_Fractions/readme.md @@ -0,0 +1,39 @@ +# Egyptian Fractions +Every positive fraction can be represented as sum of unique unit fractions. A fraction is unit fraction if numerator is 1 and denominator is a positive integer, for example 1/3 is a unit fraction. Such a representation is called Egyptian Fraction as it was used by ancient Egyptians. + +We will use _Greedy Method_ to tackle this problem. + +## Following are few examples: +- Egyptian Fraction Representation of 2/3 is 1/2 + 1/6 + +- Egyptian Fraction Representation of 6/14 is 1/3 + 1/11 + 1/231 + +- Egyptian Fraction Representation of 12/13 is 1/2 + 1/3 + 1/12 + 1/156 + +### Input Format +- In the first line, input will be the **Numerator** of the fraction. + +- In the second line, input will be the **Denominator** of the fraction. + + + +### Output Format +The Egyptian Fraction of %Numerator /%Denominator is: + +The Egyptian Fraction representation of the input fraction is now printed. + + +### Sample Input +``` +Enter the Numerator: 2 +Enter the Denominator: 3 +``` + +### Sample Output +``` +The Egyptian Fraction of 2/3 is: 1/2 + 1/6 +``` +### Implemented in: +- [Python](egyptianfractions.py) + + From dc8e0c8b2ed59363d36a2a2951d7562fda823267 Mon Sep 17 00:00:00 2001 From: Satyam Bharti <44036099+satyambharti171@users.noreply.github.com> Date: Thu, 31 Oct 2019 15:51:28 +0530 Subject: [PATCH 2/3] Create Egyptian_fractions.py --- .../Egyptian_Fractions/Egyptian_fractions.py | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 algorithms/Maths/Egyptian_Fractions/Egyptian_fractions.py diff --git a/algorithms/Maths/Egyptian_Fractions/Egyptian_fractions.py b/algorithms/Maths/Egyptian_Fractions/Egyptian_fractions.py new file mode 100644 index 0000000..0778300 --- /dev/null +++ b/algorithms/Maths/Egyptian_Fractions/Egyptian_fractions.py @@ -0,0 +1,27 @@ +import math + +def EFN(nf , df): + if(nf > 0 or df > 0): + if(df % nf == 0) + print("1/" + str(df // nf)) + return + + if(nf % df == 0): + print("{}".format(int(nr // dr))) + return + + if(df < nf): + print("{}".format(int(nf//df)),end=" + ") + EFN(nf % df, df) + return + + if(nf < df): + p=math.ceil(df / nf) + print("1/{}".format(int(p)),end=" + ") + EFN((nf * p) - df, df * p) + return + +nr = int(input("Enter the Numerator: ")) +dr = int(input("Enter the Denominator: ")) +print("The Egyptian Fraction of %d / %d is: "%(nr , dr)) +EFN(nr , dr) From c44cb2b87f70cfa66a51714a4369fed6ff99d6da Mon Sep 17 00:00:00 2001 From: Satyam Bharti <44036099+satyambharti171@users.noreply.github.com> Date: Thu, 31 Oct 2019 15:53:52 +0530 Subject: [PATCH 3/3] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e112b13..773f193 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ Happy Open Sourcing! - [Modulo Square Root](algorithms/Maths/Modulo-Square-Root) - [Sum_of_fibonacci_numbers](algorithms/Maths/Sum_of_fibonacci_numbers) - [Check_if_given_number_is_Fibonacci_number](algorithms/maths/Check_if_given_number_is_Fibonacci_number) +- [Find Egyptian Fractions for a given fraction](algorithms/Maths/Egyptian_Fractions) ### Sorting