Probably not my best obfuscation, but I'm proud of some parts of it. Takes a fraction as an argument and returns the egyptian fraction for that number.
For example:
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))))}
<Reveal this spoiler or all in this thread>
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Egyptian fractions (Golf Anyone?)
by Limbic~Region (Chancellor) on Aug 25, 2006 at 15:22 UTC | |
by liverpole (Monsignor) on Aug 25, 2006 at 15:37 UTC | |
by tadman (Prior) on Aug 25, 2006 at 17:13 UTC | |
by blokhead (Monsignor) on Aug 25, 2006 at 16:43 UTC | |
by shmem (Chancellor) on Aug 26, 2006 at 01:53 UTC | |
by dewey (Pilgrim) on Aug 26, 2006 at 03:04 UTC | |
Re: Egyptian fractions
by dewey (Pilgrim) on Aug 25, 2006 at 02:30 UTC |
Back to
Obfuscated Code