24 Game Solver

My wife teaches elementary-school math, and I’m somewhat of a math nerd, so the 24 game is right up our alley.  Basically, you’re given four numbers, and you have to find a series of operations that makes 24 from those numbers.  For example, given 1, 2, 3, and 4, you might respond that  1*2*3*4 = 24.  Some sets of numbers are harder than others (much harder).

One day, my wife and her class were having trouble solving a particularly hard set, so she emailed me for help.  It took me a while to find the answer, and all the while I was thinking to myself “Self, I’m a programmer.  Why am I doing this the hard way?”  So now I’ve created the easy (cheater) way.  Go to http://jebware.com/24 input your 4 numbers, and it will tell you how to make 24.

Right now it does addition, subtraction, multiplication, division, and exponentiation.  However, it doesn’t understand the commutative property, so you’ll get a lot of answers that are essentially the same, like (1*2)*(3*4) and (4*3)*(2*1). I wrote it in javascript, and if you want the source or you want to improve on it, I made a repository on GitHub.

Author: jeb

Views expressed here are my own and do not necessarily reflect the views of my employer.

One thought on “24 Game Solver”

Comments are closed.