clark:~/Desktop jim$ ./egypt.pl 19/20 19/20 = 1/2 + 1/3 + 1/9 + 1/180 clark:~/Desktop jim$ ./egypt.pl 18/20 18/20 = 1/2 + 1/3 + 1/15 #### sub egypt {my$f=2;do{return(--$f,egypt($_[0]/$f))unless$_[0]%$f++} while$_[0]>1}print"$ARGV[0] = ",join(' + ',pharaoh(hiero(split '/', $ARGV[0]))),"\n";sub hiero{my(%nf,%df);$nf{$_}++for egypt(shift);++ $df{$_}for egypt(shift);do{($df{$_},$nf{$_})=($df{$_}-$nf{$_},$nf{$_} -$df{$_})if$df{$_}}for keys%nf;return(eval join('*',1,(map{($_)x $nf{$_}}keys%nf)),eval join('*',1,(map{($_)x$df{$_}}keys%df)))}sub pharaoh {return$_[0]==1?"$_[0]/$_[1]":("1/".(int($_[1]/$_[0])+1), pharaoh(hiero($_[0]*(int($_[1]/$_[0])+1)-$_[1],$_[1]*(int($_[1]/ $_[0])+1))))} #### sub egypt { my $f = 2; do {return (--$f, egypt($_[0] / $f)) unless $_[0] % $f++} while $_[0] > 1; } print "$ARGV[0] = " , join( ' + ', pharaoh(hiero(split '/', $ARGV[0])) ), "\n"; sub hiero { my (%nf, %df); $nf{$_}++ for egypt(shift); $df{$_}++ for egypt(shift); do {($df{$_}, $nf{$_}) = ($df{$_} - $nf{$_}, $nf{$_} - $df{$_}) if $df{$_} } for keys %nf; return (eval join('*', 1, (map {($_) x $nf{$_}} keys %nf)), eval join('*', 1, (map {($_) x $df{$_}} keys %df))); } sub pharaoh { return $_[0] == 1 ? "$_[0]/$_[1]" : ( "1/" . (int($_[1] / $_[0]) + 1), pharaoh(hiero($_[0] * (int($_[1] / $_[0]) + 1) - $_[1], $_[1] * (int($_[1] / $_[0]) + 1)))) } #### sub egypt { my $f = 2; do {return (--$f, egypt($_[0] / $f)) unless $_[0] % $f++} while $_[0] > 1; } #### print "$ARGV[0] = " , join( ' + ', pharaoh(hiero(split '/', $ARGV[0])) ), "\n"; #### sub hiero { my (%nf, %df); $nf{$_}++ for egypt(shift); $df{$_}++ for egypt(shift); do {($df{$_}, $nf{$_}) = ($df{$_} - $nf{$_}, $nf{$_} - $df{$_}) if $df{$_} } for keys %nf; return (eval join('*', 1, (map {($_) x $nf{$_}} keys %nf)), eval join('*', 1, (map {($_) x $df{$_}} keys %df))); } #### sub pharaoh { return $_[0] == 1 ? "$_[0]/$_[1]" : ( "1/" . (int($_[1] / $_[0]) + 1), pharaoh(hiero($_[0] * (int($_[1] / $_[0]) + 1) - $_[1], $_[1] * (int($_[1] / $_[0]) + 1)))) }