in reply to Re: Egyptian fractions (Golf Anyone?)
in thread Egyptian fractions
Update: changing the entire structure to a C-style for-loop saves 2 chars, so this gives me a 79-char solution:$ 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
for(($n,$d)=split"/",pop;$n;){1while++$x<$d/$n;warn"1/$x\n";$n=$n*$x-$ +d;$d*=$x}
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:
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).perl -e '$f=eval pop;{1while++$x<1/$f;warn"1/$x\n";redo if$f-=1/$x}' 1 +9/20
blokhead
|
|---|