arekowczarek Posted December 8, 2010 Report Share Posted December 8, 2010 Until now I always refered to a certain website if I needed to calculate the number of k-element combinations within n-element set. Today I wrote a macro that does the calculation for me. It is still hot, so there are still errors due occasional division by 0 which I am gonna deal with later, and few other shortcomings. But in general it works ok. The website does the calculaton somehow for any REASONABLE n and k values while my macro will fail if it needs to calculate i.e. 25! (factorial of 25) as a part of calculating the combinations number. The website is probably (for sure) using a different algorithm to do the calculations than the one I used in my script. So, can anyone tell how does calculating combinations number look like in i.e VBA or Java? I mean, is there a native procedure (or something like this) one could use, and only enter the values of n and k, or it requires writing the whole code calculating the number of combinations step by step(in a way I did in the macro below). I know the website calculator is better at avoiding situation when an integer would receive a number larger than it can store. I just wonder whether the programmer deserves the credit for it or it is just an upside of the programming language used. The macro attached prompts for the values of n and k and provides the result. combinations calculator.mex Quote Link to comment Share on other sites More sharing options...

terrypin Posted December 8, 2010 Report Share Posted December 8, 2010 It failed my first few simple tests: n = 3 k = 2 n = 3 k = 1 n = 4 k = 1 n = 6 k = 5 I assume it fails with k = 1 for all n. And whenever n-k = 1. I'd simply get the macro to open Excel and insert n and k into the function COMBIN(). -- Terry, East Grinstead, UK Quote Link to comment Share on other sites More sharing options...

arekowczarek Posted December 8, 2010 Author Report Share Posted December 8, 2010 I assume it fails with k = 1 for all n. And whenever n-k = 1. You're right. Due the algorithm I used, while calculating combinations for k=1 and for n-k=1 division by 0 occurs. As for k=1 - it is easy to fix, cause when k=1 the combination number equals n. As for n-k=1 I know alright why the division by 0 occurs and I will deal with this later. I'd simply get the macro to open Excel and insert n and k into the function COMBIN(). In the future I will need to calculate combinations on different users' machines. Dynamically. The need of extra software used in the process is not really an option. The macro I wrote works just fine within the range of numbers I will need it to work. It won't work for ie. n=400 and k=390 but that's ok, I'm not gonna need to calculate such numbers, ever. But since the macro is written, I was just wondering how I could make the macro more varsatile, make it work with greater range of numbers. It would require a different algorithm... or I was hoping that there might exist some native procedure similar to Excel's function COMBIN() in VBA or Java or whatever language I could insert into ME script. Quote Link to comment Share on other sites More sharing options...

## Recommended Posts

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.