埃及分数被写为单位分数的总和,这意味着分子始终为1;此外,没有两个分母可以相同。创建埃及分数的一种简单方法是反复获取将适合的最大单位分数,减去以找到剩余的余数,然后重复进行直到剩余为单位分数为止,例如:
我正在尝试使用以下贪婪方法来解决埃及分数问题:
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)
我需要构建一种算法,以保证最大数量的项或最小的最大分母;例如
5 ÷ 121 = 1/25 + 1/757 + 1/763309 + 1/873960180913 +
1/1527612795642093418846225
but a simpler rendering of the same number is
1/33 + 1/121 + 1/363.