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.
Following are a 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
We can generate Egyptian Fractions using Greedy Algorithm. For a given number of the form ‘nr/dr’ where dr > nr, first find the greatest possible unit fraction, then recur for the remaining part. For example, consider 6/14, we first find ceiling of 14/6, i.e., 3. So the first unit fraction becomes 1/3, then recur for (6/14 – 1/3) i.e., 4/42.
Below is the implementation of the above idea.
C++
#include <bits/stdc++.h>
using namespace std;
void egyptianFraction( int n, int d)
{
if (d == 0 || n == 0)
return ;
if (d % n == 0) {
cout << "1/" << d / n;
return ;
}
if (n % d == 0) {
cout << n / d;
return ;
}
if (n > d) {
cout << n / d << " + " ;
egyptianFraction(n % d, d);
return ;
}
int x = d / n + 1;
cout << "1/" << x << " + " ;
egyptianFraction(n * x - d, d * x);
}
int main()
{
int numerator = 6, denominator = 14;
cout << "Egyptian Fraction representation of "
<< numerator << "/" << denominator << " is"
<< endl;
egyptianFraction(numerator, denominator);
return 0;
}
|
Java
class GFG {
static void printEgyptian( int nr, int dr)
{
if (dr == 0 || nr == 0 ) {
return ;
}
if (dr % nr == 0 ) {
System.out.print( "1/" + dr / nr);
return ;
}
if (nr % dr == 0 ) {
System.out.print(nr / dr);
return ;
}
if (nr > dr) {
System.out.print(nr / dr + " + " );
printEgyptian(nr % dr, dr);
return ;
}
int n = dr / nr + 1 ;
System.out.print( "1/" + n + " + " );
printEgyptian(nr * n - dr, dr * n);
}
public static void main(String[] args)
{
int nr = 6 , dr = 14 ;
System.out.print(
"Egyptian Fraction Representation of " + nr
+ "/" + dr + " is\n " );
printEgyptian(nr, dr);
}
}
|
Python3
import math
def egyptianFraction(nr, dr):
print ( "The Egyptian Fraction " +
"Representation of {0}/{1} is" .
format (nr, dr), end = "\n" )
ef = []
while nr ! = 0 :
x = math.ceil(dr / nr)
ef.append(x)
nr = x * nr - dr
dr = dr * x
for i in range ( len (ef)):
if i ! = len (ef) - 1 :
print ( " 1/{0} +" .
format (ef[i]), end = " " )
else :
print ( " 1/{0}" .
format (ef[i]), end = " " )
egyptianFraction( 6 , 14 )
|
C#
using System;
class GFG
{
static void printEgyptian( int nr, int dr)
{
if (dr == 0 || nr == 0)
return ;
if (dr % nr == 0)
{
Console.Write( "1/" + dr / nr);
return ;
}
if (nr % dr == 0)
{
Console.Write(nr / dr);
return ;
}
if (nr > dr)
{
Console.Write(nr / dr + " + " );
printEgyptian(nr % dr, dr);
return ;
}
int n = dr / nr + 1;
Console.Write( "1/" + n + " + " );
printEgyptian(nr * n - dr, dr * n);
}
public static void Main()
{
int nr = 6, dr = 14;
Console.Write( "Egyptian Fraction Representation of " +
nr + "/" + dr + " is\n " );
printEgyptian(nr, dr);
}
}
|
Javascript
<script>
function printEgyptian(nr, dr)
{
if (dr == 0 || nr == 0)
return ;
if (dr % nr == 0)
{
document.write( "1/" + parseInt(dr / nr, 10));
return ;
}
if (nr % dr == 0)
{
document.write(parseInt(nr / dr, 10));
return ;
}
if (nr > dr)
{
document.write(parseInt(nr / dr, 10) + " + " );
printEgyptian(nr % dr, dr);
return ;
}
let n = parseInt(dr / nr, 10) + 1;
document.write( "1/" + n + " + " );
printEgyptian(nr * n - dr, dr * n);
}
let nr = 6, dr = 14;
document.write( "Egyptian Fraction Representation of " + nr + "/" + dr + " is\n " );
printEgyptian(nr, dr);
</script>
|
PHP
<?php
function printEgyptian( $nr , $dr )
{
if ( $dr == 0 || $nr == 0)
return ;
if ( $dr % $nr == 0)
{
echo "1/" , (int) $dr / $nr ;
return ;
}
if ( $nr % $dr == 0)
{
echo (int)( $nr / $dr );
return ;
}
if ( $nr > $dr )
{
echo (int)( $nr / $dr ), " + " ;
printEgyptian( $nr % $dr , $dr );
return ;
}
$n = (int)( $dr / $nr ) + 1;
echo "1/" , $n , " + " ;
printEgyptian( $nr * $n - $dr , $dr * $n );
}
$nr = 6;
$dr = 14;
echo "Egyptian Fraction Representation of " ,
$nr , "/" , $dr , " is\n " ;
printEgyptian( $nr , $dr );
?>
|
OutputEgyptian Fraction representation of 6/14 is
1/3 + 1/11 + 1/231
Time complexity: The time complexity of the given algorithm depends on the value of the input fraction. In the worst case, the algorithm will run for O(d) iterations, where d is the denominator of the input fraction. Therefore, the time complexity of the algorithm is O(d).
Auxiliary Space:The space complexity of the given algorithm is O(1) as it uses only a fixed amount of extra space to store the variables used in the algorithm, regardless of the input size.
The recursive solution in Python is as follows:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > getEgyptianFractionUtil( int numerator, int denominator,
vector< int > listOfDenoms)
{
if (numerator == 0)
return listOfDenoms;
int newDenom = ceil (( double )denominator / numerator);
listOfDenoms.push_back(newDenom);
listOfDenoms = getEgyptianFractionUtil(
numerator * newDenom - denominator,
newDenom * denominator, listOfDenoms);
return listOfDenoms;
}
string getEgyptianFraction( int numerator, int denominator)
{
string str = "" ;
vector< int > output = getEgyptianFractionUtil(numerator, denominator, {});
for ( auto denom : output)
str += "1/" + to_string(denom) + " + " ;
string strCopy = str.substr(0, str.length() - 3);
return strCopy;
}
int main()
{
cout << getEgyptianFraction(6, 14);
return 0;
}
|
Java
import java.util.*;
public class Main {
static Vector<Integer>
getEgyptianFractionUtil( int numerator, int denominator,
Vector<Integer> listOfDenoms)
{
if (numerator == 0 )
return listOfDenoms;
int newDenom = ( int )Math.ceil(( double )denominator
/ numerator);
listOfDenoms.add(newDenom);
listOfDenoms = getEgyptianFractionUtil(
numerator * newDenom - denominator,
newDenom * denominator, listOfDenoms);
return listOfDenoms;
}
static String getEgyptianFraction( int numerator,
int denominator)
{
String str = "" ;
Vector<Integer> output = getEgyptianFractionUtil(
numerator, denominator, new Vector<Integer>());
for ( int denom : output)
str += "1/" + Integer.toString(denom) + " + " ;
String strCopy = str.substring(
0 ,
str.length() - 3 );
return strCopy;
}
public static void main(String[] args)
{
System.out.println(getEgyptianFraction( 6 , 14 ));
}
}
|
Python3
import math
def getEgyptianFraction(numerator, denominator):
str = ""
output = getEgyptianFractionUtil(numerator, denominator, [])
for denom in output:
str + = "1/{0} + " . format (denom)
strCopy = str [: - 3 ]
return strCopy
def getEgyptianFractionUtil(numerator, denominator, listOfDenoms):
if numerator = = 0 :
return listOfDenoms
newDenom = math.ceil(denominator / numerator)
listOfDenoms.append(newDenom)
listOfDenoms = getEgyptianFractionUtil(numerator * newDenom - denominator,
newDenom * denominator, listOfDenoms)
return listOfDenoms
print (getEgyptianFraction( 6 , 14 ))
|
Javascript
function getEgyptianFractionUtil(numerator, denominator, listOfDenoms) {
if (numerator == 0) {
return listOfDenoms;
}
let newDenom = Math.ceil((denominator / numerator));
listOfDenoms.push(newDenom);
listOfDenoms = getEgyptianFractionUtil(numerator * newDenom - denominator, newDenom * denominator, listOfDenoms);
return listOfDenoms;
}
function getEgyptianFraction(numerator, denominator) {
let str = "" ;
let output = getEgyptianFractionUtil(numerator, denominator, []);
for (let denom of output) {
str += "1/" + denom + " + " ;
}
let strCopy = str.substring(0, str.length - 3);
return strCopy;
}
console.log(getEgyptianFraction(6, 14));
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
namespace GFG {
class Program {
static IEnumerable< int >
getEgyptianFractionUtil( int numerator, int denominator,
IEnumerable< int > listOfDenoms)
{
if (numerator == 0)
return listOfDenoms;
int newDenom = ( int )Math.Ceiling(( double )denominator
/ numerator);
listOfDenoms
= listOfDenoms.Concat( new int [] { newDenom });
listOfDenoms = getEgyptianFractionUtil(
numerator * newDenom - denominator,
newDenom * denominator, listOfDenoms);
return listOfDenoms;
}
static string getEgyptianFraction( int numerator,
int denominator)
{
string str = "" ;
IEnumerable< int > output = getEgyptianFractionUtil(
numerator, denominator, new int [] {});
foreach ( int denom in output) str
+= "1/" + denom.ToString() + " + " ;
string strCopy = str.Substring(
0, str.Length - 3);
return strCopy;
}
static void Main( string [] args)
{
Console.WriteLine(getEgyptianFraction(6, 14));
}
}
}
|
Time complexity The time complexity of the given code is O(N^2), where N is the maximum number of denominators in the Egyptian fraction representation of the given fraction. This is because the algorithm recursively calculates each denominator in the fraction representation, and each recursive call takes O(N) time.
Space complexity :The space complexity of the given code is O(N), where N is the maximum number of denominators in the Egyptian fraction representation of the given fraction. This is because the algorithm uses a vector to store the denominators, which can have at most N elements.
The Greedy algorithm works because a fraction is always reduced to a form where denominator is greater than numerator and numerator doesn’t divide denominator. For such reduced forms, the highlighted recursive call is made for reduced numerator. So the recursive calls keep on reducing the numerator till it reaches 1.
This article is contributed by Shubham. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.