Egyptian Fraction Problem

Here given code implementation process.

//C Program
//Egyptian Fraction Problem
#include <stdio.h>

void egyptian_fraction(int a,int b)
{
  if(a==0 || b == 0)
  {
    return;
  }
  if(b >= a)
  {
    //Find calculate remainder
    int remainder = b % a;
    
    //Find calculate divisor
    int divisor   = b / a;
   
    if(remainder != 0)
    {
      divisor++;

      printf("1/%d + ",divisor );

      a = a * divisor - b;

      b = b * divisor; 

      egyptian_fraction(a,b);

    }
    else
    {
      printf("1/%d ",divisor );
    }
  }
  else
  {
    printf("%d + ",a/b );
    
    egyptian_fraction(a % b, b); 
  }
}

int main()
{
  
  int a = 7, b = 53;
 
  egyptian_fraction(a,b);

  return 0;
}

Output

1/8 + 1/142 + 1/30104
/*
 C++ Program
 Egyptian Fraction Problem
*/

#include<iostream>

using namespace std;
class GreedyAlgo {
  public:
    void egyptian_fraction(int a, int b) {
      if (a == 0 || b == 0) {
        return;
      }
      if (b >= a) {
        int remainder = b % a;
        int divisor = b / a;
        if (remainder != 0) {
          divisor++;
          cout << "1/" << divisor << " + ";
          a = a *divisor - b;
          b = b *divisor;
          this->egyptian_fraction(a, b);
        } else {
          cout << "1/" << divisor;
        }
      } else {
        cout << (a / b) << " + ";
        this->egyptian_fraction(a % b, b);
      }
    }
};
int main() {
  GreedyAlgo obj;
  int a = 7;
  int b = 53;
  obj.egyptian_fraction(a, b);
  return 0;
}

Output

1/8 + 1/142 + 1/30104
/*
  Java program
  Egyptian Fraction Problem
*/

public class GreedyAlgo {

  public void egyptian_fraction(int a,int b)
  {
    if(a==0 || b == 0)
    {
      return;
    }
    if(b >= a)
    {
      //Find calculate remainder
      int remainder = b % a;
      
      //Find calculate divisor
      int divisor   = b / a;
     
      if(remainder != 0)
      {
        divisor++;

        System.out.print("1/"+divisor+" + " );

        a = a * divisor - b;

        b = b * divisor; 

        egyptian_fraction(a,b);

      }
      else
      {
        System.out.print("1/"+divisor );
      }
    }
    else
    {
      System.out.print((a/b) +" + ");
      
      egyptian_fraction(a % b, b); 
    }
  }
  public static void main(String[] args) {

    GreedyAlgo obj = new GreedyAlgo();
    //two numbers
    int a = 7;
    int b = 53;
    obj.egyptian_fraction(a,b);

  }
}

Output

1/8 + 1/142 + 1/30104
/*
  C# program
  Egyptian Fraction Problem
*/
using System;
public class GreedyAlgo {

  public void egyptian_fraction(int a,int b)
  {
    if(a==0 || b == 0)
    {
      return;
    }
    if(b >= a)
    {
      //Find calculate reMainder
      int reMainder = b % a;

      //Find calculate divisor
      int divisor   = b / a;

      if(reMainder != 0)
      {
        divisor++;

        Console.Write("1/"+divisor+" + " );

        a = a * divisor - b;

        b = b * divisor; 

        egyptian_fraction(a,b);

      }
      else
      {
        Console.Write("1/"+divisor );
      }
    }
    else
    {
      Console.Write((a/b) +" + ");

      egyptian_fraction(a % b, b); 
    }
  }
  public static void Main(String[] args) {

    GreedyAlgo obj = new GreedyAlgo();
    //two numbers
    int a = 7;
    int b = 53;
    obj.egyptian_fraction(a,b);

  }
}

Output

1/8 + 1/142 + 1/30104
# Python 3 Program
# Egyptian Fraction Problem

class GreedyAlgo :
  def egyptian_fraction(self, a, b) :
    if (a == 0 or b == 0) :
      return
    
    if (b >= a) :
      remainder = b % a
      divisor = int( b / a )
      if (remainder != 0) :
        divisor += 1
        print("1/{} + ".format(divisor),end="")
        a = a * divisor - b
        b = b * divisor
        self.egyptian_fraction(a, b)
      else :
        print("1/{}".format(divisor),end="")
      
    else :
      print(int(a / b) ," + ",end="")
      self.egyptian_fraction(a % b, b)
    
  

def main() :
  obj = GreedyAlgo()
  a = 7
  b = 53
  obj.egyptian_fraction(a, b)

if __name__ == "__main__":
  main()

Output

1/8 + 1/142 + 1/30104
# Ruby Program
# Egyptian Fraction Problem

class GreedyAlgo 
  def egyptian_fraction(a, b) 
    if (a == 0 or b == 0) 
      return
    end
    if (b >= a) 
      remainder = b % a
      divisor = b / a
      if (remainder != 0) 
        divisor += 1
        print("1/", divisor ," + ")
        a = a * divisor - b
        b = b * divisor
        self.egyptian_fraction(a, b)
      else 
        print("1/", divisor)
      end
    else 
      print((a / b) ," + ")
      self.egyptian_fraction(a % b, b)
    end
  end
end
def main() 
  obj = GreedyAlgo.new()
  a = 7
  b = 53
  obj.egyptian_fraction(a, b)
end
main()

Output

1/8 + 1/142 + 1/30104
<?php
/*
 Php Program
 Egyptian Fraction Problem
*/

class GreedyAlgo {
  public  function egyptian_fraction($a, $b) {
    if ($a == 0 || $b == 0) {
      return;
    }
    if ($b >= $a) {
      $remainder = $b % $a;
      $divisor = intval($b / $a);
      if ($remainder != 0) {
        $divisor++;
        echo("1/". $divisor ." + ");
        $a = $a *$divisor - $b;
        $b = $b *$divisor;
        $this->egyptian_fraction($a, $b);
      } else {
        echo("1/". $divisor);
      }
    } else {
      echo(intval($a / $b) ."  ");
      $this->egyptian_fraction($a % $b, $b);
    }
  }
}

function main() {
  $obj = new GreedyAlgo();
  $a = 7;
  $b = 53;
  $obj->egyptian_fraction($a, $b);
}
main();

Output

1/8 + 1/142 + 1/30104
/*
 Node Js Program
 Egyptian Fraction Problem
*/

class GreedyAlgo {
  egyptian_fraction(a, b) {
    if (a == 0 || b == 0) {
      return;
    }
    if (b >= a) {
      var remainder = b % a;
      var divisor = parseInt(b / a);
      if (remainder != 0) {
        divisor++;
        process.stdout.write("1/" + divisor + " + ");
        a = a *divisor - b;
        b = b *divisor;
        this.egyptian_fraction(a, b);
      } else {
        process.stdout.write("1/" + divisor);
      }
    } else {
      process.stdout.write(parseInt(a / b) + " + ");
      this.egyptian_fraction(a % b, b);
    }
  }
}

function main() {
  var obj = new GreedyAlgo();
  var a = 7;
  var b = 53;
  obj.egyptian_fraction(a, b);
}

main();

Output

1/8 + 1/142 + 1/30104
/*
 Swift 4 Program
 Egyptian Fraction Problem
*/

class GreedyAlgo {
  func egyptian_fraction(_ actual_a: Int, _ actual_b: Int) {
    var a = actual_a;
    var b = actual_b;
    if (a == 0 || b == 0) {
      return;
    }
    if (b >= a) {
      let remainder: Int = b % a;
      var divisor: Int = b / a;
      if (remainder != 0) {
        divisor += 1;
        print("1/\(divisor) + ",terminator:"");
        a = a * divisor - b;
        b = b * divisor;
        self.egyptian_fraction(a, b);
      } else {
        print("1/\(divisor)",terminator:"");
      }
    } else {
      print((a / b) ,"+",terminator:"");
      self.egyptian_fraction(a % b, b);
    }
  }
}
func main() {
  let obj: GreedyAlgo = GreedyAlgo();
  let a: Int = 7;
  let b: Int = 53;
  obj.egyptian_fraction(a, b);
}
main();

Output

1/8 + 1/142 + 1/30104
/*
  Scala program
  Egyptian Fraction Problem
*/
class GreedyAlgo {
  def egyptian_fraction(new_a: Int, new_b: Int): Unit = {
        var a = new_a;
        var b = new_b;
    if (a == 0 ||
      b == 0) {
      return;
    }
    if (b >= a) {
      //Find calculate remainder
      var remainder: Int = b % a;

      //Find calculate divisor
      var divisor: Int = (b / a).toInt;

      if (remainder != 0) {
        divisor += 1;
        print("1/" + divisor + " + ");
        a = a * divisor - b;
        b = b * divisor;
        egyptian_fraction(a, b);
      } else {
        print("1/" + divisor);
      }
    } else {
      print(""+((a / b).toInt) + " + ");
      egyptian_fraction(a % b, b);
    }
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    var obj: GreedyAlgo = new GreedyAlgo();

    //two numbers
    var a: Int = 7;
    var b: Int = 53;
    obj.egyptian_fraction(a, b);
  }
}

Output

1/8 + 1/142 + 1/30104

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved