Greedy algorithm for Egyptian fractions: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: bigatom -> mpfr)
Line 1,693: Line 1,693:
procedure efrac(integer num, denom)
procedure efrac(integer num, denom)
string prefix = ""
string fraction = sprintf("%d/%d",{num,denom}),
prefix = ""
if num>=denom then
if num>=denom then
integer whole = floor(num/denom)
integer whole = floor(num/denom)
Line 1,700: Line 1,701:
end if
end if
string e = join(egyptian(num, denom)," + ")
string e = join(egyptian(num, denom)," + ")
printf(1,"%d/%d -> %s%s\n",{num,denom,prefix,e})
printf(1,"%s -> %s%s\n",{fraction,prefix,e})
end procedure
end procedure
Line 1,754: Line 1,755:
43/48 -> 1/2 + 1/3 + 1/16
43/48 -> 1/2 + 1/3 + 1/16
5/121 -> 1/25 + 1/757 + 1/763309 + 1/873960180913 + 1/1527612795642093418846225
5/121 -> 1/25 + 1/757 + 1/763309 + 1/873960180913 + 1/1527612795642093418846225
8/59 -> [34] + 1/8 + 1/95 + 1/14947 + 1/670223480
2014/59 -> [34] + 1/8 + 1/95 + 1/14947 + 1/670223480


for proper fractions with 1 to 2 digits
for proper fractions with 1 to 2 digits