- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Correction!
Admin
My solution to this: don't fret about lost nickels and just learn poker.
Admin
Ahh f**k it, the line should be:
long count = pow(OPERATORS, size - 1);
As long as it's big enough, it will work...
Admin
For those talking about other games with a similar idea, there is an Avalon Hill dice game from 1967, Tuf, but that included dice for the operators.
Admin
In highschool one of my best friends came up with a similar game which we refined a bit later on. The idea was to add arithmetic operators to the numbers on the licence plates of the passing cars. There was a bit of a rule list to keep the game challenging yet not impossible.
Admin
Oh, I think I wrote that a bit badly. I meant that you had to add mathematical operators AND a single equality sign and get a valid equation.
Admin
I'd gather they are married to each other...
Admin
I guess my bonus points go to you, sir ;-)
Admin
TRWTF is how much of my work day I spent on this.
I have generated all the possible formulas, but I'm not going to bother going to the trouble of writing a math evaluator in C#. If I only cared a bit more there are some freeware evaluators available, but then if I'm going to start downloading stuff there is probably a solution for this problem too...
Admin
Where's the WTF?
I've got enough programming exercises at work thank you.
Admin
FORTY-TWO!
No, doesn't fit. DAMN!
Admin
Actually, he was correct. The best way to tell is if the sentence reads correctly without the "you" and the "and".
"super-genius coders like I" is not correct. Therefore, the full sentence should be "Presumably not all of the readers of this site are super-genius coders like you and me"
Admin
Admin
Admin
Admin
inb4 one-line perl/python/ruby implementation
Admin
I came up with a "hard" solution in Python. It's rather long, so refer to Pastebin. Example usage:
In response to the fellow who asked what the hardest 24 puzzle is, I always thought of 1,3,4,6 as the hardest. I won't spoil it here but if you get stuck you can always run my solver. :)Admin
Married to who?
Captcha: damnum
Admin
I get "TypeError: 'Fraction' object cannot be interpreted as an index". And I suck at python, so I have no idea what to do there :P.
Admin
Admin
I loved Krypto as a kid. My problem was that I could never find anyone willing to play with me*.
*Seems to be an ailment that has spilled over to adult life!
Admin
Admin
I (ab)used Lua for a 24 line, 734 byte Medium-Hard implementation. A lot of duplicate solutions are removed, and all solutions are found from any length string. For good measure it also includes power and modulo operators.
http://pastebin.com/VxcpdnrL
If your lua binary is called lua.exe and the file "stupid.lua", run with the param string "lua.exe stupid.lua 5 5 8 2 10 24" to solve alex's 24 card problem.
Admin
I knew I should have previewed that first.
Code.
Damn spamblockers.
Admin
1,3,4,6 = 24 has no solution using the four operators and paranthesis. Here is a brute force solution in your favorite language, vb6:
Public Sub Solve(ByVal a1 As Integer, ByVal a2 As Integer, ByVal a3 As Integer, ByVal a4 As Integer, ByVal a5 As Integer, ByVal Answer As Integer)
Dim n As Long Dim x As Long Dim y As Long
Dim operator(3) As String Dim pos(3) As String Dim z As Long Dim s(44) As String Dim a(4) As Long
a(0) = a1 a(1) = a2 a(2) = a3 a(3) = a4 a(4) = a5
operator(0) = "+" operator(1) = "-" operator(2) = "*" operator(3) = "/"
s(0) = " " 'two s(1) = "( ) " s(2) = " ( ) " s(3) = " ( ) " s(4) = " ( )" s(5) = "( )( ) " s(6) = "( ) ( )" s(7) = " ( )( )"
s(8) = "( ) " 'three s(9) = " ( ) " s(10) = " ( )"
s(11) = "[ ) ) " 'first three s(12) = "( ( ] " s(13) = " [ ) ) " 'middle three s(14) = " ( ( ] " s(15) = " [ ) )" 'last three s(16) = " ( ( ]"
s(17) = "( )( )" 'three and two s(18) = " ) )( )" s(19) = "( ( "
s(20) = "( )( )" 'two and three s(21) = "( )[ ) )" s(22) = "( )( ( ]"
s(23) = "( ) " 'first four s(24) = "[ ) ) " 'first four and two s(25) = "( ( ) ) " s(26) = "( ( ] " s(27) = "[ )( ] "
s(28) = "[ ) ) " 'first four and first three s(29) = "{ ) ) ) " s(30) = "[ ( ] ) "
s(31) = "( ( ] " 'first four and last three s(32) = "( [ ) ] " s(33) = "( ( ( } "
s(34) = " ( )" 'last four s(35) = " [ ) )" 'last four and two s(36) = " ( ( ) )" s(37) = " ( ( ]" s(38) = " [ )( ]"
s(39) = " [ ) )" 'last four and first three s(40) = " { ) ) )" s(41) = " [ ( ] )"
s(42) = " ( ( ]" 'last four and last three s(43) = " ( [ ) ]" s(44) = " ( ( ( }"
For n = 0 To 255 z = n For x = 0 To 3 pos(x) = operator(3 And z) z = z \ 4 Next x For z = 0 To 44 Dim ss As String ss = "" For y = 0 To 4 Dim zz zz = Mid(s(z), y * 2 + 1, 1) If zz = "[" Then zz = "((" ElseIf zz = "{" Then zz = "(((" End If ss = ss & zz & a(y) zz = Mid(s(z), y * 2 + 2, 1) If zz = "]" Then zz = "))" ElseIf zz = "}" Then zz = ")))" End If ss = ss & zz If y <> 4 Then ss = ss & " " & pos(y) & " " End If Next y Dim ans As Double On Error Resume Next ans = script.Eval(ss) On Error GoTo 0 Debug.Print ss & " = " & ans
Next n
If Not bFoundAnswer Then MsgBox "No solution." End If
Dim v For Each v In col 'show all unique possible positive integer solutions < 25 Debug.Print v Next
End Sub
Admin
Unless you are assuming 1 may equal 1 OR 11:
11 + 3 + 4 + 6 = 24
Admin
Actually there is a legitimate way to get 1,3,4,6 to 24 within the rules of the game, but it requires some out of the box thinking...
6 / (1 - (3/4)) = 24
i.e. 6 / 0.25 = 24
Disclaimer: I didn't figure this out myself.
Admin
Good job, I'm sorry, I did not look at the cards to see that they could be re-ordered. I also don't know if and didn't assume you could 'group cards' without multiplication; e.g., "8s 5d" could be used as "85"
Admin
Actually the very first person was correct. The real trick is to add the missing verb. "Presumably not all of the readers of this site are super-genius coders like you and I are." And then take out the "are": "Presumably not all of the readers of this site are super-genius coders like you and I."
Admin
Let's see... given n cards, there's a total of n!*4^(n-1)*fibonacci(n) possible patterns, that's 153600 different patterns for n=5... for n=10, it's 52319748096000... a brute force algorithm checking all patterns might not scale so easily.
Admin
_24.cc. Handles harder problems like 1, 3, 4, 6 using a swap hack (labelled r as in the Unix utility dc) at the expense of many more duplicate solutions!
First solution illustrated:
3 4 / 1 r - 6 r / 0.75 1 r - 6 r / 1 0.75 - 6 r / 0.25 6 r / 6 0.25 / 24
Overall, I found the challenge interesting. Obviously next_permutation is the workhorse here, and the reason I used C++. I haven't touched C++ in a while, so got to flex some old brain muscles...
Admin
And I'll throw in an actual reference I found when double checking the song lyrics. http://grammar.quickanddirtytips.com/between-you-and-me.aspx
Odd, posting comments breaks when javascript is off.
Admin
Easy! We just need "something" that has all the cards, and then something that deals cards randomly. Then "something else" to look at how those somethings could be put together mathematically.
It doesn't take Someone like Kevin to solve this problem. Sheesh!
Admin
I agree. After recoding for n cards in any order; for 5 (unique) cards I get the following:
Card Count: 5 Card permutations count: 120 Parenthesis permutations count: 38 Operator permutations count: 256 Total equation count: 1167360
The only thing not shown by this is elimating parenthesis where commutative property is true.
Admin
Here's a pop quiz for you:
Tony is very smart. I wish I was smart like a) he b) him
Paula is brilliant. I'm glad I'm not brilliant like a) she b) her
You and I are strong. Everybody wishes they were strong like a) you and I b) you and me
The would-be grammarians who offered their improvements to the original post were a) wrong b) completely off base c) trying so hard to show off their cleverness it makes you wonder a little about their self-esteem, the poor dears. Maybe someone should have praised them more when they were children, or something.
(choose all that apply)
(captcha: uxor - like XOR, only more so)
Admin
I tried it on the 1,3,4,6 = 24 problem and it didn't find the answer (there's exactly one solution not using power or mod).
Admin
Submitted previous comment prematurely...
That might need some more work. It tends to produce a lot of duplicates with only minor differences, such as X+Y+Z and X+(Y+Z), and misses a lot of solutions, including (510+8)/2-5, (2-10)/8+55, (2*10-5)8/5, (5/2+5/10)8, (2+5+5)(10-8) and 2(5+5+10-8).I tried it on the 1,3,4,6 = 24 problem and it didn't find the answer (there's exactly one solution not using power or mod).
Admin
A couple years ago I was programming for an insurance company. On fridays, a couple actuaries and I would spend an hour at a local elementary school teaching math to kids.
One game we played was 24. The class would be divided into teams, and a member from each team would go to the board. 4 numbers were called out and the first person to use all four numbers to make 24 would get a point.
One day, we decided to play a game where the kids all played against the actuaries. I was a programmer, so I argued to be on my own team. I immediately pulled out my laptop and began hacking together a ruby a script. I ignored the first few rounds, but 5 minutes later I was ready to start playing.
I taught those kids an important lesson in the power of automation that day.
Admin
I wrote a script to run my solver over all 4- and 5-digit input arrays and pick out the inputs that are "hardest", judging by the number of solutions and the operators used. One seems to stand out above all others:
6,7,7,7,8 = 24 has two solutions. One is 678/(7+7). What's the other one?
Another interesting problem is 5,5,5,5,5 = 24. One solution is (555-5)/5. What's the other one?
Admin
As for removing duplicates - it does a pretty poor job on this I know. It's hard to do better then that though without writing good code, which I was trying to avoid.
Akismet swears this is spam.
Admin
Admin
Agh, have to stop rushing these posts. Here's the correct pastebin. 932 bytes, 32 lines.
Admin
I'm disappointed no one tried using prolog.
Admin
Go ahead, I for one would like to see a prolog solution..
And my final submission. 3rd time's the charm..
Admin
Here is PHP code with eval, division by 0, global variables and of course random! See it in action!
Admin
Great site.
However, recursion is the more elegant solution to this sort of problem to me.
Admin
You guys don't try hard enough. Here's a fragment I wrote in C. It's incomplete, but I'm sure you get the idea.
Admin
I wrote essentially the "Hard" version many years ago (in C, later in ML and the Prolog) to find all the solutions to the "Numbers" game from the British intellectual TV show Countdown.
It works like this:
On a computer, this is very easy to do by exhaustive search - even with mid 1980's desktop tech it takes about 2-3 seconds if coded well.
http://en.wikipedia.org/wiki/Countdown_%28game_show%29
Admin
(5 - (5 / (5 * 5))) * 5 = 24
Admin
Which of these do you like?
6 / ((7 + 7) / 7) * 8 = 24 6 * 7 / ((7 + 7) / 8) = 24 6 / ((7 + 7) / 7 / 8) = 24 6 / ((7 + 7) / 8) * 7 = 24 6 / ((7 + 7) / 8 / 7) = 24 6 * 8 / ((7 + 7) / 7) = 24 7 * 6 / ((7 + 7) / 8) = 24 7 / ((7 + 7) / 6) * 8 = 24 7 / ((7 + 7) / 6 / 8) = 24 7 / ((7 + 7) / 8) * 6 = 24 7 / ((7 + 7) / 8 / 6) = 24 7 * 8 / ((7 + 7) / 6) = 24 8 * 6 / ((7 + 7) / 7) = 24 8 / ((7 + 7) / 6) * 7 = 24 8 / ((7 + 7) / 7) * 6 = 24 8 * 7 / ((7 + 7) / 6) = 24 8 / ((7 + 7) / 7 / 6) = 24