Skip to content

Instantly share code, notes, and snippets.

@mshock
Created May 15, 2012 15:04
Show Gist options
  • Save mshock/2702464 to your computer and use it in GitHub Desktop.
Save mshock/2702464 to your computer and use it in GitHub Desktop.
golf - n choose p
# current solution:
<>=~/,/;$r=++$_+$`;while($c++-$'){$_=$r/$c*$_-$_}print
######################################################
=pod
All of my scratch getting to this point:
<>=~/,/;$r=++$_+$`;while($c++-$'){$_=$_*$r/$c-$_}print
while
$_=<>;$c=1;s/(\d+)/$1?$c*=$1-1:''/e while/\d/;print$c;
join'*',1..$1,1..$2,1..$2-$1
s!(\d+), (\d+)!(join'*',$1-$2+1..$1).'/'.join'*',1..$2!e
y!0-9!/!c
s!,!/!;
#!perl -p s/(\d+), (\d+)/join'*',$..$1/e;eval;
s/(\d+), (\d+)/join'*',1..$1,'/(',1..$2,')',1..$1-$2,')'/
s/(\d+)/join'*',$..$1/e;y/(\d+)/$1/s;
<>=~/,/;$r=++$_+$`;while($c++-$'){$_=$r/$c*$_-$_}print
<>=~/,/;$r=++$_+$`;while($c++-$'){$_*=($r/$c-1)}print
<>=~/,/;$r=$`+1;$_=1;while($c++-$'){$_=$_*$r/$c-$_}print
<>=~/,/;$r=++$_+$`;while($c++-$'){$_=$r/$c*$_-$_}print
<>=~/,/;$r=++$_+$`;while($c++-$'){$_=$_*($r/$c)-$_}print
<>=~/,/;$r=++$_+$`;while($c++-$'){$_=$_*($r/$c)-$_}print
<>=~/,/;$r=++$_+$`;while($c++-$'){$_=$_*$r/$c-$_}print
<>=~/,/;$r=++$_+$`;while($c++-$'){$_=$_*($r/$c)-$_}print
<>=~/,/;@a=map{eval join'*',1..0+$_||1}($`,$',$`-$');print $a[0]/($a[1]*$a[2])
<>=~/,/;sub c{eval join'*',1..0+pop||1}print c($`)/(c($')*c$`-$')
#!perl -F, -ap $r=$F[0]+1;$_=1;while($c++!=$F[1]){$_=$_*$r/$c-$_}
#!perl -F, -ap ($r,$c)=@F;while($c--){$_=$_*$r/$c-$_}
<>=~/,/;$r=$`+1;sub c{$_=pop;$c++==$'?print:c($_*$r/$c-$_)}c$.
<>=~/,/;$r=$`+1;$_=1;sub c{$c++==$'?print:$_=$_*$r/$c-$_;c}c
<>=~/,/;$r=$`+1;$_=1;while($c++!=$'){$_*=($r/$c-1)}print
<>=~/,/;$r=$`+1;$_=1;while($c++!=$'){$_*=($r/$c-1)}print
#!perl -p /,/;$r=$`+1;$_*=$r/$c-$_ if$c++==$';
<>=~/,/;sub c{eval join'*',1..0+pop||1}print c($`)/(c($')*c$`-$')
<>=~/,/;$r=$`+1;sub c{$_=pop;$c++==$'?print:c($_*$r/$c-$_)}c$.
<>=~/,/;sub c{($_,$r,$c)=@_;$c++==$'?print:c($_*$r/$c-$_,$r,$c)}c(1,$`+1,0)
<>=~/,/;$r=$`+1;sub c{($_,$c)=@_;$c++==$'?print:c($_*$r/$c-$_,$c)}c(1,0)
<>=~/,/;sub c{($v,$r,$c)=@_;$c++==$'?print$v:c($v*($r/$c-1),$r,$c)}c(1,$`+1,0)
<>=~/,/;sub c{($v,$r,$c)=@_;$c++==$'?print$v:c($v*$r/$c-$v,$r,$c)}c(1,$`+1,0)
#!perl -p /,/;}{
<>=~/, (\d+)/;sub c{$_[0]?$_[0]*c($_[0]-1):1}print c($`)/(c($1)*c($`-$1))
<>=~/,/;do{(my$t=$_)=~s/(\d)/join'*',1..$1||1/eg};
<>=~/,/;s/(\d)/join'*',1..$1||1/eg;print eval
<>=~/,/;print map{join'*',1..0+$_||1}($`,$',$`-$')
<>=~/,/;$_="$`/($'*".($`-$').")";s/(\d)/join'*',1..$1||1/eg;print eval
<>=~/,/;$a=$`-$';print eval "$`/($'*$a)"=~s/(\d)/join'*',1..$1||1/egr
<>=~/,/;$c="$`/($'*($`-$'))";$t=~s/(\d)/join'*',1..$1||1/g;print eval$t;
<>=~/(\d+)..(\d+)/;sub c{$a*=$_ for(1..$_[0])}print c($1)/(c($2)*c($1-$2))
<>=~/, (\d+)/;sub c{eval join'*',(1..pop)}print c($`)/(c($1)*c($`-$1))
<>=~/,/;sub c{eval join'*',1..0+pop||1}print c($`)/(c($')*c($`-$'))
<>=~/,/;sub c{eval join'*',pop..0+pop||1}print(1/c$',1)*c$`,$`-$'+1
<>=~/,/;print map{join'*',1..0+$_||1}($`,$',$`-$')
<>=~/,/;sub c{join'*',1..0+pop||1}eval"print c($`)/(c($')*c($`-$'))"
<>=~/, (\d+)/;sub c{$_[0]?$_[0]*c$_[0]-1:1}print c($`)/(c($1)*c($`-$1))
c($`)/(c($1)*c($`-$1))
c($`,$1)/c($`-$1,1)
sub c{$_[0]?$_[0]*c($_[0]-1):1}
sub c{($a,$b)=@_;$a==$b?$a:c($a-1,$b)}
sub c{$_[0]?$_[0]*c($_[0]-1):1}print c($`)/(c($1)*c($`-$1))
<>=~/, (\d+)/;$1=~s/(\d+)/$1-1*/ eval
# 83
#perl -F, -ape
#sub c{$_[0]?$_[0]*c($_[0]-1):1}($n,$k)=@F;$_=c($n)/(c($k)*c($n-$k))
#sub c{$_[0]?$_[0]*c($_[0]-1):1}$_=c($F[0])/(c($F[1])*c($F[0]-$F[1]))
#sub c{$_[0]?$_[0]*c($_[0]-1):1}$_=c($1)/(c($2)*c($1-$2))
# 78
#<>=~/(\d+)..(\d+)/;sub c{$_[0]?$_[0]*c($_[0]-1):1}print c($1)/(c($2)*c($1-$2))
=cut
@mshock
Copy link
Author

mshock commented May 15, 2012

Still working on getting this shorter. I'm sure that a pure regex solution is the way to go, I really need to avoid the loss of precision that this solution introduces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment