Converting Seven Card Positions to a Single Number
When this newsletter is published it will be four days before the total eclipse. I hope you are all excited and all of you who are physically and financially capable of witnessing it in the totality path will. Unfortunately, the weather in Texas, where I plan to view it, has thunderstorms in the forecast for that day. However, I remain hopefully for clear skies.
Before getting to the main topic, I had a surprising strong response to my last newsletter on easter trivia. It would seem some of you know your bible quite well and challenged me on some of my answers. Here were the two main questions protested, which are closely related:
Questions 8: Judas later returned the money to the priests he was paid to betray Jesus. What did they do with the money?
Answer 8: They bought a potter’s field, which is where unclaimed bodies are buried. (Matthew 27:6- 8)
It was pointed out to me that the book of Acts tells a different story of what happened to the 30 pieces of Silver. There is says that Judas purchased the potter’s field himself (Acts 1:18)
Question 9: How did Judas kill himself?
Answer 9: Hanging (Matthew 27:3-5)
Again, the book of Acts seems to have a different version. I’m not exactly sure what to make of it, so I’ll let scripture speak for itself: “With the payment he received for his wickedness, Judas bought a field; there he fell headlong, his body burst open and all his intestines spilled out. “ – Acts 1:18.
My interpretation of this is Judas got sick, becoming bloated with open sores. Eventually he became so weak, he fell down and his intestines burst out. It is possible the fall was intentional.
In researching this, I see some advocates of biblical inerrancy have gone to great lengths to try to conflate the two stories into one convoluted one. However, I can only roll my eyes at such explanations.
With that out of the way, let’s get to the main topic for this week about efficient coding to score a seven-card poker hand. This may not seem a big problem, given the speed of computers. However, Ultimate Texas Hold ‘Em entails analyzing 56 trillion ways the cards can fall. Each one of them requires scoring two seven-card poker hands. This could take years without short cuts.
There are combin(52,7) = 133,784,560 ways to choose seven cards out of a 52-card deck. An important time-saving technique is to score every possible combination once and save the scores into an array. However, how does get from seven individual cards to a single location in an array?
Before giving my answer, one might suggest using a seven-dimensional array of size 52^7. Such an array would need to hold 1,028,071,702,528 integers. I don’t think any desktop computer will allow such much memory allocation. No, we need to somehow map 7 cards to an integer from 0 to 133,784,559. My C++ compiler allows arrays of such size without complaining.
First, attribute each card to an integer from 0 to 51. You may do this any way you like. Personally, I start with the 2’s as values 0 to 3, 3’s are 4 to 7, and so on up to aces ranging from 48 to 51. It is important that when you divide a card number by 4 that the remainder consistently gives the same suit. For example, all hearts might have a modulo of 0, spaces 1, clubs 2 and diamonds 3.
My function will map the lowest set of cards 0,1,2,3,4,5,6 to the number 0. Likewise, the maximum set of 45,46,47,48,49,50,51 will be mapped to the maximum value of 133,784,559.
By way of example, let’s consider the set of cards numbered 5,10,15,20,25,30,35 and determine what integer that set should be mapped to.
First, consider the lowest card numbered 5. We can skip over a lot of combinations involving at least one card numbered 0 to 4. The number of ways to choose 7 cards out of the remaining 47 in the deck is combin(47,7) = 62,891,499. As mentioned earlier, there are combin(52,7)=133,784,560 ways to choose 7 cards out of 52. So, we can skip over 133,784,560 - 62,891,499 = 70,893,061 numbers that have at least one card in the range of 0 to 4.
Next, consider the second card with a value of 10. We can skip over more combinations of that involve at least one card in the range of 6 to 9. The number of ways to choose the 6 cards left out of the 42 left in the deck is combin(42,6) = 5,245,786. This is out of combin(46,6)=9,366,819 possible ways to choose the other six cards above the value of 5 of the first card. So, we can skip over 9,366,819-5,245,786=4,121,033 numbers for sets of cards that include at least one value between 6 and 9.
Next, consider the third card with a value of 15. We can skip over more combinations that involve at least one card in the range of 11 to 14. The number of ways to choose the 5 cards left out of the 37 left in the deck is combin(37,5) = 435,897. This is out of combin(41,5)=749,398 possible ways to choose the other five cards above the value of 10 of the second card. So, we can skip over 749,398-435,897=313,501 numbers for sets of cards that include at least one value between 11 and 14.
Next, consider the fourth card with a value of 20. We can skip over more combinations that involve at least one card in the range of 16 to 19. The number of ways to choose the 4 cards left out of the 32 left in the deck is combin(32,4) = 35,960. This is out of combin(36,4)=58,905 possible ways to choose the other four cards above the value of 15 of the third card. So, we can skip over 58,905-35,960=22,945 numbers for sets of cards that include at least one value between 16 and 19.
Next, consider the fifth card with a value of 25. We can skip over more combinations that involve at least one card in the range of 21 to 24. The number of ways to choose the three cards left out of the 27 left in the deck is combin(27,3) = 2,925. This is out of combin(31,3)=4,495 possible ways to choose the other three cards above the value of 20 of the fourth card. So, we can skip over 4,495-2,925=1,570 numbers for sets of cards that include at least one value between 21 and 24.
Next, consider the sixth card with a value of 30. We can skip over more combinations that involve at least one card in the range of 26 to 29. The number of ways to choose the two cards left out of the 22 left in the deck is combin(22,2) = 231. This is out of combin(26,3)=325 possible ways to choose the other two cards above the value of 20 of the fifth card. So, we can skip over 325-231=94 numbers for sets of cards that include at least one value between 26 and 29.
We are now down to just one card. We from the sixth card it must be greater than 30. In fact, we know it is 35. So, we can skip over the four cards numbered 31 to 34 for the seventh card.
In total, we have skipped over 70,893,061+4,121,033+313,501+22,945+1,570+94+4 = 75,352,208 cards.
Since we start numbering at zero, like all good programmers do, we can assign the value to the set of numbers 5,10,15,20,25,30,35 to the number 75,352,208.
Of course, this same logic will work for any size deck and choosing any number of cards from it.
Next week I plan to give you at least a preliminary report about the total eclipse of April 8, 2024!