Several TI-Basic programs have been created for the the 24 Game. The goal of this game is to manipulate four integers from 1-9 so that the result is 24. You can use the four common math operators (+,-,*,/). In addition, you can use parentheses/brackets to group operations. The following is an example of a valid solution: 4+8/(2/5)=24.
Instead of creating another port, I wanted to create a solver. This prompted me to start thinking about it.
Here is my code right now.
Code:
This program is testing each possible solution by changing each operator one at a time, then the parentheses, then the order of the numbers. It will test all 24576 possible solutions.
It will solve for most solutions. However, there are some drawbacks to this. Sometimes, it will test duplicate solutions. Sometimes, it will test for solutions such as A-(B)/(C+D. Sometimes, it will error in a divide by zero error. This usually happens when there are duplicate numbers in the 4 numbers or when the expression has a divide by zero such as 5/(9-6-3).
I am looking for a faster way to test for a solution. Here is something that I found to be helpful. However, I am unsure of which is the best method nor do I know how to duplicate the method into TI-Basic.http://rosettacode.org/wiki/24_game/Solve
Thanks!
Instead of creating another port, I wanted to create a solver. This prompted me to start thinking about it.
Here is my code right now.
Code:
Prompt A,B,C,D
"+-*/→Str1
"1()→Str2
"ABCD→Str4
[[1,2,3,4][1,2,4,3][1,3,2,4][1,3,4,2][1,4,2,3][1,4,3,2][2,1,3,4][2,3,4,1][2,1,4,3][2,4,3,1][2,4,1,3][2,3,1,4][3,1,2,4][3,4,2,1][3,2,1,4][3,2,4,1][3,4,1,2][3,1,4,2][4,1,2,3][4,1,3,2][4,2,3,1][4,3,1,2][4,3,2,1][4,2,1,3
Ansᵀ→[A]
//((A
//E first operation
//H first group (can not be closing parentheses) cannot be 3
//B
//I second group (cannot be opening parentheses) cannot be 2
//F second operation
//J third group (can not be closing parentheses) cannot be 3
//C
//K fourth group (cannot be opening parentheses) cannot be 2
//G third operation
//D
ClrHome
Disp "
For(L,1,24)
Matr►list([A],L,L₁
For(H,1,2)
For(I,1,2)
2I-1→I
For(J,1,2)
For(K,1,2)
2K-1→K
For(E,1,4)
For(F,1,4)
For(G,1,4)
"(("+sub(Str4,L₁(1),1)+sub(Str1,E,1
If H≠1:Ans+sub(Str2,H,1
Ans+sub(Str4,L₁(2),1
If I≠1:Ans+sub(Str2,I,1
Ans+sub(Str1,F,1
If J≠1:Ans+sub(Str2,J,1
Ans+sub(Str4,L₁(3),1)
If K≠1:Ans+sub(Str2,K,1
Ans+sub(Str1,G,1)+sub(Str4,L₁(4),1→Str3
Output(1,1,Str3+"
If 24=expr(Str3:Disp Str3
End
End
End
End
End
End
End
End
This program is testing each possible solution by changing each operator one at a time, then the parentheses, then the order of the numbers. It will test all 24576 possible solutions.
It will solve for most solutions. However, there are some drawbacks to this. Sometimes, it will test duplicate solutions. Sometimes, it will test for solutions such as A-(B)/(C+D. Sometimes, it will error in a divide by zero error. This usually happens when there are duplicate numbers in the 4 numbers or when the expression has a divide by zero such as 5/(9-6-3).
I am looking for a faster way to test for a solution. Here is something that I found to be helpful. However, I am unsure of which is the best method nor do I know how to duplicate the method into TI-Basic.http://rosettacode.org/wiki/24_game/Solve
Thanks!