I'm looking at analyzing a test. It consists of four questions with ten options each. From each question, three options must be selected. That of course always yields a total of 12 answers.
Analyzing the number of possible combinations using Ruby's [].combination my Dell Workstations freezes up and fails to produce sane values. Is that calculation really so extremely intense/large?
Using 40 options over 12 answers, I figured I should run:
[0...39].combination(12){|x| p x }
I also found the Wikipedia article on Combination. But being handicapped in terms of math, it really did not make my any smarter.
I would madly appreciate any help I can get on this matter. Thanks everybody.
Secondary/follow-up question:
Plus points to anyone who can figure out a smart way to generate all possible combinations. It's needed for a "offline" Ruby application simulating all possible answers. My brain is dizzy from analyzing the docs – but I cannot seem to find an efficient way to achieve this task.
Ideally, I would need one giant array containing ints representing the options selected:
[[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]
...would for example represent a scenario where question one, two, three and fou开发者_如何学编程r was answered using options 0, 1 and 2.
The code you provided is asking for all the ways of selecting 12 things from 40 things which is a huge number (hence why your machine freezes), but, from the way you described your situation I don't think that's right.
Each question should be handled separately, that is, you want the number of ways of choosing 3 things from 10 things:
[0...9].combination(3) {|x| p x }
would print out all the different ways of doing so This gives all the ways of answering a single question (which will be the same for all the questions).
This also should run a lot faster since it's a much smaller calculation.
You then multiply the number of combinations for each question to get the total number of ways of answering all four questions.
My calculation gives:
120 combinations per question.
120 * 120 * 120 * 120 = 207,360,000 combinations for the whole test.
What you have is four sets of "Choose 3 of 10" combinations.
Please please, oh, please god don't solve this by actually producing all the possible combinations! That's why man invented mathematics :-)
One set of "10 choose three" has a number of options equal to (as shown on the wikipedia page you linked to):
10! / ((10-3)! * 3!)
= 10! / (7! * 3!)
= (10! / 7!) / 3!
= (10 * 9 * 8) / (3 * 2 * 1)
= 720 / 6
= 120
You have four of those questions, so you multiply the number of possible combinations in each question to get the total number of combinations for the test.
120 * 120 * 120 * 120
= 207,360,000
...just as Jonathan showed, above.
I'm not sure, but 10 possible answers, choosing 4, would give 210 possibilities.
Then, matching question 1&2 would give 210 * 210 = 44.100 possibilities. On the other side, matching questions 3&4 would give other 44.100 possibilites.
Matching 1&2 + 3&4 would be 1.944.810.000 possibilites
That is quite a lot of possibilites...
But your idea of 12 between 40 would give a bigger number, of about 5.586.853.480.
Usually going brute-fore over combinatory is not a good idea...
PS: I'm not sure of the accuracy of my numbers... studied this long time ago
精彩评论