in reply to Re: Egyptian fractions (Golf Anyone?)
in thread Egyptian fractions

81 chars:
$ perl -e '($n,$d)=split"/",pop;{1while++$x<$d/$n;warn"1/$x\n";$n=$n*$ +x-$d;$d*=$x;redo if$n}' 18/20 1/2 1/3 1/15
Update: changing the entire structure to a C-style for-loop saves 2 chars, so this gives me a 79-char solution:
for(($n,$d)=split"/",pop;$n;){1while++$x<$d/$n;warn"1/$x\n";$n=$n*$x-$ +d;$d*=$x}
This uses the greedy heuristic, so it doesn't find the "best" Egyptian fractions. $x keeps increasing until 1/$x is <= our current fraction. Then we print 1/$x and subtract from the current fraction. But there is no need to put this fraction in lowest terms, which saves a lot of strokes.

If POSIX::ceil were available, 1while++$x<$d/$n is really just $x=ceil($d/$n) ..

BTW, this 58-char solution would work if it weren't for floating-point error:

perl -e '$f=eval pop;{1while++$x<1/$f;warn"1/$x\n";redo if$f-=1/$x}' 1 +9/20
I don't have Math::Pari where I am, but it may be possible to use it in the above approach (it treats rationals with absolute precision).

blokhead