>>1
Did you mean: fact n = product [2..n]
perm n r = product [n-r+1..n]
comb n r = let sr = min r (n-r) in
perm n sr `div` fact sr
Name:
Anonymous2009-05-24 19:58
I recommend java and use a CombinationsFactory
Name:
Anonymous2009-05-24 20:05
Is it just me, or does LISP make everything 10x longer and harder to write? And you're telling me this is the language which the almighty programmers use? Please, don't make me laugh.
Name:
Anonymous2009-05-24 20:08
>>6
agreed. satori-grade languages should surely be terse as fuck
Name:
Anonymous2009-05-24 20:09
>>6
You're not using enough abstraction, if a procedure is more than 4 lines long YOU'RE DOING IT WRONG
import java.util.*;
import java.io.*;
public class Facts {
private static final double[] facts = {1.0,1.0,2.0,6.0,24.0,120.0,720.0,5040.0,40320.0,362880.0,3628800.0,3.99168E7,4.790016E8,6.2270208E9,8.71782912E10,1.307674368E12,2.0922789888E13,3.55687428096E14,6.402373705728E15,1.21645100408832E17,2.43290200817664E18,5.109094217170944E19,1.1240007277776077E21,2.585201673888498E22,6.204484017332394E23,1.5511210043330986E25,4.0329146112660565E26,1.0888869450418352E28,3.0488834461171384E29,8.841761993739701E30,2.6525285981219103E32,8.222838654177922E33,2.631308369336935E35,8.683317618811886E36,2.9523279903960412E38,1.0333147966386144E40,3.719933267899012E41,1.3763753091226343E43,5.23022617466601E44,2.0397882081197442E46,8.159152832478977E47,3.3452526613163803E49,1.4050061177528798E51,6.041526306337383E52,2.6582715747884485E54,1.1962222086548019E56,5.5026221598120885E57,2.5862324151116818E59,1.2413915592536073E61,6.082818640342675E62,3.0414093201713376E64,1.5511187532873822E66,8.065817517094388E67,4.2748832840600255E69,2.308436973392414E71,1.2696403353658276E73,7.109985878048635E74,4.052691950487722E76,2.350561331282879E78,1.3868311854568986E80,8.320987112741392E81,5.075802138772248E83,3.146997326038794E85,1.98260831540444E87,1.2688693218588417E89,8.247650592082472E90,5.443449390774431E92,3.647111091818868E94,2.4800355424368305E96,1.711224524281413E98,1.197857166996989E100,8.504785885678622E101,6.123445837688608E103,4.4701154615126834E105,3.3078854415193856E107,2.480914081139539E109,1.8854947016660498E111,1.4518309202828584E113,1.1324281178206295E115,8.946182130782973E116,7.156945704626378E118,5.797126020747366E120,4.75364333701284E122,3.945523969720657E124,3.314240134565352E126,2.8171041143805494E128,2.4227095383672724E130,2.107757298379527E132,1.8548264225739836E134,1.6507955160908452E136,1.4857159644817607E138,1.3520015276784023E140,1.24384140546413E142,1.1567725070816409E144,1.0873661566567424E146,1.0329978488239052E148,9.916779348709491E149,9.619275968248206E151,9.426890448883242E153,9.33262154439441E155,9.33262154439441E157,9.425947759838354E159,9.614466715035121E161,9.902900716486175E163,1.0299016745145622E166,1.0813967582402903E168,1.1462805637347078E170,1.2265202031961373E172,1.3246418194518284E174,1.4438595832024928E176,1.5882455415227421E178,1.7629525510902437E180,1.9745068572210728E182,2.2311927486598123E184,2.543559733472186E186,2.925093693493014E188,3.3931086844518965E190,3.969937160808719E192,4.6845258497542883E194,5.574585761207603E196,6.689502913449124E198,8.09429852527344E200,9.875044200833598E202,1.2146304367025325E205,1.5061417415111404E207,1.8826771768889254E209,2.372173242880046E211,3.012660018457658E213,3.8562048236258025E215,4.9745042224772855E217,6.466855489220472E219,8.471580690878817E221,1.118248651196004E224,1.4872707060906852E226,1.992942746161518E228,2.6904727073180495E230,3.659042881952547E232,5.01288874827499E234,6.917786472619486E236,9.615723196941086E238,1.346201247571752E241,1.89814375907617E243,2.6953641378881614E245,3.8543707171800706E247,5.550293832739301E249,8.047926057471987E251,1.17499720439091E254,1.7272458904546376E256,2.5563239178728637E258,3.808922637630567E260,5.7133839564458505E262,8.627209774233235E264,1.3113358856834518E267,2.006343905095681E269,3.089769613847349E271,4.789142901463391E273,7.47106292628289E275,1.1729568794264138E278,1.8532718694937338E280,2.946702272495037E282,4.714723635992059E284,7.590705053947215E286,1.2296942187394488E289,2.0044015765453015E291,3.2872185855342945E293,5.423910666131586E295,9.003691705778433E297,1.5036165148649983E300,2.526075744973197E302,4.2690680090047027E304,7.257415615307994E306};
public static double getFactorial(int n) {
if(n<0 || n>=facts.length) throw new IndexOutOfBoundsException();
return facts[n];
}
public static double getCombinations(int n, int k) {
if(n<0 || k<0 || n>=facts.length) throw new IndexOutOfBoundsException();
else if(k>n) throw new IllegalArgumentException();
return facts[n]/(facts[k]*facts[n-k]);
}
public static double getPermutations(int n, int r) {
if(n<0 || r<0 || n>=facts.length) throw new IndexOutOfBoundsException();
else if(r>n) throw new IllegalArgumentException();
return facts[n]/facts[n-r];
}
public static void main(String[] args) {
System.out.println("52C5: "+getCombinations(52,5));
System.out.println("10P3: "+getPermutations(10,3));
System.out.println("5!: "+getFactorial(5));
}
}
Name:
Anonymous2009-05-24 20:29
>>6
It's just you, unless you're using Forth or APL as your baseline.
Name:
Anonymous2009-05-24 21:30
private static final double[] facts ENTERPRISE QUALITY
Is it just me, or does LISP make everything 10x longer and harder to write?
Actually the original is very short but makes unnecessary intermediate calculations like >>4 . When I was trying to calculate 20000C10000 it was taking too many precious seconds and I had to OPTIMIZE it.