Category Archives: mathematica

Solving 24s

I am a fan of the mathematical card game “24s”, in which four cards are turned over simultaneously, and all the players search for ways to make the number 24 by combining the four numbers shown with any mathematical operators they like (I have always played with addition, subtraction, multiplication, division, powers, roots, and logarithms, but the list can be made longer or shorter depending on the preferences and mathematical skill of the players). The number on every card must be used, and no card can be used more than once. Face cards are pulled from the deck before playing and aces == 1. Suits don’t matter for the game; the deck is being used only as a random number generator.

The game is addictively fun, combining elements of math, pattern recognition, and memory. I learned the game in high school, and used to play with expedition-mates when climbing high mountains, to test for loss of mental acuity at altitude. Recently, I’ve been teaching my daughter how to play. She does pretty well!

Solutions are possible for most hands (about 83% of hands, for the normal rule set, by my computation), and in most solvable cases, more than one solution is possible. In case of a tie, cards can be distributed back to the players, or victory can be given to the newest player, or to the person who came up with the most solutions, or the most interesting solution.

Take for example the hand 2,3,10,10.

hand of cards

Two solutions are possible. The “easy” one is


But the more impressive one, remembered decades later who saw it played, is


I mention this game because I recently realized that the game is solvable, and that so long as you restrict yourself to binary operators, it is possible to determine absolutely which hands can be turned into 24 and which can’t, and how many solutions are possible for each hand, and what those solutions are.

I’ll discuss my method in future posts.

Mathematica and MySQL Server

I often use Mathematica and MySQL together; it’s typically a lot more efficient than trying to keep megabytes of data in the Mathematica notebook itself, and becomes vastly more powerful if I want to use the same data from multiple machines, all of which can tap into the same database.

Mathematica supports these uses natively, and you can send arbitrary SQL code to the server with the SQLExecute command. Interestingly, Mathematica also provides commands that map to major SQL commands, including SQLSelect[] and SQLInsert[]. I have never used these, viewing it as easier to program SQL commands in SQL itself, tuning them in MySQL Workbench.

Today, it occurred to me that Mathematica’s built-in commands might somehow be quicker than sending raw SQL, and I tested this hypothesis. Surprisingly, it turns out to be the case, with results similar to the following for a number of cases.

In[1]:= Needs["DatabaseLink`"]

In[2]:= commodDB = OpenSQLConnection[JDBC["MySQL(Connector/J)", "localhost:3306/commoddb"],
Username -> "myUsername", Password -> "myPassword"];

In[3]:= Timing[
SQLSelect[commodDB, "tbcommodprices", {"date", "ticker", "price"},
SQLColumn["ticker"] == "LAV08 Comdty" && SQLColumn["whichprice"] == "last"];]

Out[3]= {0.031, Null}

In[4]:= Timing[
SQLExecute[commodDB, "SELECT date, ticker, price FROM `commoddb`.`tbcommodprices` where ticker='LAV09 Comdty' and whichprice='last'"];]

Out[4]= {0.047, Null}

(The two queries differ slightly because I did not want caching to give an advantage to the second query).

Mathematica at StackExchange

There is a proposal to create a Mathematica section at the popular programming Q&A site StackExchange. I think it’s probably a good idea; the mathgroup mailing list has been a great resource for years but the web is easier and quicker than e-mail sometimes.

Sign up to support it here:
Stack Exchange Q&A site proposal: Mathematica

(Or, if the link above is busted, just visit

Amusing Youth With A Computable Document Format

A few months ago, Wolfram proposed the Computable Document Format, a standard format for files with computable content. If you have a CDF reader, you can execute the computations on your own machine. At this point, only Wolfram itself makes software for producing or reading CDFs, and I see no signs of rapid adoption, though it may be taking off in educational communities or others to which I have little exposure.

If you have Wolfram’s Computable Document Player, or Plugin installed on your computer, you should be able to use the following bit of code, which I created to amuse my daughter after the “day of the week your birthday falls on” code failed to do so.

colorList = ColorData["Indexed", "ColorList"][[1]];

Rotate[Style[Text["CHILD'S NAME HERE"], FontSize -> size,
FontColor -> colorList[[Round[color]]]], rotation], {{size, 40},
30, 120}, {rotation, 0, 2*Pi}, {color, 1, Length[colorList]}]

[WolframCDF source=”” CDFwidth=”600″ CDFheight=”600″ altimage=””]

I actually modified it for the CDF, combining the two lines into a single Module[], which the CDF player seems to prefer, and giving a choice of three names. You might think it would be best to include a text entry field so that the program can display any name you like, but the CDF player is Mathematica at heart, if you feed it a solvable math problem as the name, it will try to solve it. Wolfram therefore disallows free-form text entry.