[thechat] Programming Problem

Beau Hartshorne beau at members.evolt.org
Thu Nov 8 23:12:30 CST 2001


> the number of distinct combinations of four cards drawn from 
> a playing card deck is 52*51*50*49 = 6,497,400
> 
> if you disregard suits, there are fewer combos, but i can't 
> be bothered counting them -- you can't just divide 6,497,400 
> by 4, because there's only one hand with all sevens, but 144 
> different hands with two sevens and two eights....  urggle...

13*12*11*10 = 17160 if suits don't count.

What I wanted to do was write a program that could take 4 numbers as
input, then output all the different ways that they successfully
add/multiply/divide/subtract to 24 -- not something that just spat out
every single possible combination of cards and operators. The program
would have to try every single possible combination of operators and
parenthesis to find all the cases where the four numbers input would add
to 24. If it was written in C or even Java, this would take a few
seconds for the computer to solve, at the most.

> if the number of operators is 4, and they don't have to be 
> distinct, then there are 4**3 = 64 ways of combining four 
> cards (i.e. using three infix operators), but then the 
> possibility of parentheses throws everything into a cocked hat

Are there any other ways to arrange the parentheses (where it would
affect the order of operations):

((a-b)-c)-d
(a-(b-c))-d
(a-b)-c-d
(a-b-c)-d
(a-b)-(c-d)
a-(b-c)-d
a-((b-c)-d)
a-(b-(c-d))
a-(b-c-d)
a-b-(c-d)

Where each "-" represents some operator. If those are the only ways to
arrange the parentheses, then the program just needs to iterate through
every possible combination of operators, and every possible order of
operands to solve the problem.

Beau





More information about the thechat mailing list