📜  埃及分数的贪婪算法

📅  最后修改于: 2021-05-04 14:56:44             🧑  作者: Mango

每个正分数都可以表示为唯一单位分数的总和。如果分子是1并且分母是正整数,则分数是单位分数,例如1/3是单位分数。这种表示法称为古埃及人使用的埃及分数。
以下是一些示例:

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

我们可以使用贪婪算法生成埃及分数。对于给定数量的形式’nr / dr’,其中dr> nr,首先找到最大可能的单位分数,然后递归其余部分。例如,考虑6/14,我们首先找到14/6的上限,即3。因此,第一个单位分数变为1/3,然后递归(6/14 – 1/3),即4/42。
以下是上述想法的实现。

C++
// C++ program to print a fraction in Egyptian Form using Greedy
// Algorithm
#include 
using namespace std;
 
void printEgyptian(int nr, int dr)
{
    // If either numerator or denominator is 0
    if (dr == 0 || nr == 0)
        return;
 
    // If numerator divides denominator, then simple division
    // makes the fraction in 1/n form
    if (dr%nr == 0)
    {
        cout << "1/" << dr/nr;
        return;
    }
 
    // If denominator divides numerator, then the given number
    // is not fraction
    if (nr%dr == 0)
    {
        cout << nr/dr;
        return;
    }
 
    // If numerator is more than denominator
    if (nr > dr)
    {
        cout << nr/dr << " + ";
        printEgyptian(nr%dr, dr);
        return;
    }
 
    // We reach here dr > nr and dr%nr is non-zero
    // Find ceiling of dr/nr and print it as first
    // fraction
    int n = dr/nr + 1;
    cout << "1/" << n << " + ";
 
    // Recur for remaining part
    printEgyptian(nr*n-dr, dr*n);
 }
 
// Driver Program
int main()
{
    int nr = 6, dr = 14;
    cout << "Egyptian Fraction Representation of "
         << nr << "/" << dr << " is\n ";
    printEgyptian(nr, dr);
    return 0;
}


Java
//Java program to print a fraction
// in Egyptian Form using Greedy
// Algorithm
 
class GFG {
 
    static void printEgyptian(int nr, int dr) {
        // If either numerator or
        // denominator is 0
        if (dr == 0 || nr == 0) {
            return;
        }
 
        // If numerator divides denominator,
        // then simple division makes
        // the fraction in 1/n form
        if (dr % nr == 0) {
            System.out.print("1/" + dr / nr);
            return;
        }
 
        // If denominator divides numerator,
        // then the given number is not fraction
        if (nr % dr == 0) {
            System.out.print(nr / dr);
            return;
        }
 
        // If numerator is more than denominator
        if (nr > dr) {
            System.out.print(nr / dr + " + ");
            printEgyptian(nr % dr, dr);
            return;
        }
 
        // We reach here dr > nr and dr%nr
        // is non-zero. Find ceiling of
        // dr/nr and print it as first
        // fraction
        int n = dr / nr + 1;
        System.out.print("1/" + n + " + ");
 
        // Recur for remaining part
        printEgyptian(nr * n - dr, dr * n);
    }
 
// Driver Code
    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);
    }
}
 
/*This code is contributed by Rajput-Ji*/


Python3
# Python3 program to print a fraction
# in Egyptian Form using Greedy
# Algorithm
 
# import math package to use
# ceiling function
import math
 
# define a function egyptianFraction
# which receive parameter nr as
# numerator and dr as denominator
def egyptianFraction(nr, dr):
 
    print("The Egyptian Fraction " +
          "Representation of {0}/{1} is".
                format(nr, dr), end="\n")
 
    # empty list ef to store
    # denominator
    ef = []
 
    # while loop runs until
    # fraction becomes 0 i.e,
    # numerator becomes 0
    while nr != 0:
 
        # taking ceiling
        x = math.ceil(dr / nr)
 
        # storing value in ef list
        ef.append(x)
 
        # updating new nr and dr
        nr = x * nr - dr
        dr = dr * x
 
    # printing the values
    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 = " ")
 
# calling the function
egyptianFraction(6, 14)
 
# This code is contributed
# by Anubhav Raj Singh


C#
// C# program to print a fraction
// in Egyptian Form using Greedy
// Algorithm
using System;
 
class GFG
{
static void printEgyptian(int nr, int dr)
{
    // If either numerator or
    // denominator is 0
    if (dr == 0 || nr == 0)
        return;
 
    // If numerator divides denominator,
    // then simple division  makes
    // the fraction in 1/n form
    if (dr % nr == 0)
    {
        Console.Write("1/" + dr / nr);
        return;
    }
 
    // If denominator divides numerator,
    // then the given number is not fraction
    if (nr % dr == 0)
    {
        Console.Write(nr / dr);
        return;
    }
 
    // If numerator is more than denominator
    if (nr > dr)
    {
        Console.Write(nr / dr + " + ");
        printEgyptian(nr % dr, dr);
        return;
    }
 
    // We reach here dr > nr and dr%nr
    // is non-zero. Find ceiling of
    // dr/nr and print it as first
    // fraction
    int n = dr / nr + 1;
    Console.Write("1/" + n + " + ");
 
    // Recur for remaining part
    printEgyptian(nr * n - dr, dr * n);
}
 
// Driver Code
public static void Main()
{
    int nr = 6, dr = 14;
    Console.Write( "Egyptian Fraction Representation of " +
                                 nr + "/" + dr + " is\n ");
    printEgyptian(nr, dr);
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


PHP
 $dr)
    {
        echo (int)($nr/$dr), " + ";
        printEgyptian($nr % $dr, $dr);
        return;
    }
 
    // We reach here dr > nr and dr%nr is
    // non-zero. Find ceiling of dr/nr and
    // print it as first fraction
    $n = (int)($dr / $nr ) + 1;
    echo "1/" , $n , " + ";
 
    // Recur for remaining part
    printEgyptian($nr * $n - $dr, $dr * $n);
}
 
// Driver Code
$nr = 6;
$dr = 14;
echo "Egyptian Fraction Representation of ",
                    $nr, "/", $dr, " is\n ";
printEgyptian($nr, $dr);
     
// This code is contributed by ajit.
?>


Javascript


输出:

Egyptian Fraction Representation of 6/14 is
 1/3 + 1/11 + 1/231 

贪婪算法之所以有效,是因为分数总是被简化为分母大于分子且分子不除分母的形式。对于这种简化形式,将对突出显示的递归调用进行简化的分子。因此,递归调用会不断减少分子,直到达到1。