这是我用Python编写的埃及分数的代码,如果还有改进的地方,我需要您的反馈。
def eg_fr(n, d):
ls = []
ls_str = []
fraction = n / d
total = 0
for i in range(1, 1000):
ls.append((1/i, str(1) + '/' + str(i)))
for i in ls:
while i[0] + total <= fraction:
ls_str.append(i[1])
total += i[0]
if total >= fraction:
break
print('%s/%s = ' % (n, d), end='')
for i in range(len(ls_str) - 1):
print('', ls_str[i], '+ ', end='')
print(ls_str[-1], end='')
return ls_str
if __name__ == '__main__':
eg_fr(7, 133)