Skip to content
This repository was archived by the owner on Nov 26, 2019. It is now read-only.

Added Egyptian Fractions implemented in Python. #183

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 27 additions & 0 deletions algorithms/Maths/Egyptian_Fractions/Egyptian_fractions.py
Original file line number Diff line number Diff line change
@@ -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)
39 changes: 39 additions & 0 deletions algorithms/Maths/Egyptian_Fractions/readme.md
Original file line number Diff line number Diff line change
@@ -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)