PDA

View Full Version : algorithm to create all 7card hands?


carlosblack
07-15-2003, 09:11 PM
Hello everybody, incredible forums with so much good info!

I was wondering if anybody had any ideas on what an algorithm to create all possible seven card combinations ignoring position (so As Ad Ah Ac 2c 2d 2h is the same as 2c 2d 2h As Ad Ah Ac) and how many combinations this will create?

I have tried an iterative approach but it is hard to ignore duplicates. I create all combinations for as ad ah ac 2s 2d xx for example, then when I go to create as ad ah ac 2s 3d xx I run into the as ad ah ac 2s 3d 2d dupe, this gets exponentially worse as I progress. Checking every single variation against existing variations seems to be the only way to do this iterativelly but it seems silly. Any ideas appreciated.

mostsmooth
07-15-2003, 10:37 PM
this is just a guess

52!/(52-7)!*7!

doormat
07-16-2003, 01:06 AM
kevin,
from a programming standpoint you would simply nest 7 loops. If the array deck[52] contains the cards, then:
for a = 1 to 46
for b = a+1 to 47
for c = b+1 to 48
for d = c+1 to 49
for e = d+1 to 50
for f = e+1 to 51
for g = f+1 to 52
card1 = deck[a]
|
card7 = deck[g]
etc.
this will give you all 133,784,560 unique hands

doormat

Copernicus
07-16-2003, 02:28 PM
Good guess (other than a slight notation problem...its either "/7!" or you need parens around everything after your "/"

The result is over 133 million.

carlosblack
07-16-2003, 05:02 PM
Thank you! A very elegant solution and just what I was looking for.

mostsmooth
07-16-2003, 06:15 PM
Good guess (other than a slight notation problem...its either "/7!" or you need parens around everything after your "/"

The result is over 133 million.

huh?
please type it as both the ways you are explaining?

Huh
07-16-2003, 11:22 PM

Copernicus
07-18-2003, 07:22 AM
most...its not really important, just an "order of operations" error in your notation:

Yours: 52!/(52-7)!*7! while the *7! appears to be in the denominator, by the rules of order of operations the three factorial functions are sequentially divided then multiplied.

Correct 1: 52!/((52-7)!*7!) now performing the multiplication in the denominator before the division

Correct 2: 52!/(52-7)!/7! now sequentially dividing by the last two factorials not dividing then multiplying.