A Lua cluster of 24 programs

Source: Internet
Author: User
Tags codepad

The last time I saw a question of "getting together 24", I couldn't think of it--B. So I wrote a program to calculate it... Now I know the answer...

 

This program does not use search or recursion. The idea is to use the reverse Polish formula to enumerate all possible expressions (this manual enumeration). There are five types:

11 + 1 + 1 +
11 + 11 + +
111 + + 1 +
111 + 1 ++
1111 ++

The above is a valid inverse Polish pattern consisting of four numbers and three operators. "1" indicates a number, while "+" indicates an operator. Then enumerate every number and operator. Total enumeration: 5*4! * 4 ^ 3 = 7680, the enumeration volume is very small, so you just need to raise the limit.

 

Code:

-- Calculate the inverse Polish formula <br/> -- (table)-> Number <br/> function evalpo (E) <br/> Local s ={}-- a stack <br/> Local a, B, c <br/> For I, V in ipairs (E) DO <br/> If type (v) = "Number" then <br/> table. insert (S, v) <br/> elseif type (V) = "string" then <br/> If # S <2 then <br/> error (not enough values ') <br/> end <br/> B = table. remove (s) <br/> A = table. remove (s) <br/> If v = '+' Then <br/> C = a + B <br/> el Seif v = '-'Then <br/> C = a-B <br/> elseif v =' * 'Then <br/> C = a * B <br /> elseif v = '/' Then <br/> If B = 0 then <br/> return nil, 'divided by 0' <br/> else <br/> C = A/B <br/> end <br/> else <br/> error ('bad OPERATOR: '.. v) <br/> end <br/> table. insert (S, C) <br/> end <br/> If # S ~ = 1 then <br/> return nil, 'Not Enough operators' <br/> end <br/> return s [1] <br/> end </P> <p> -- converts an inverse polish expression to a normal expression., output <br/> -- (table)-> string <br/> function rpntoexp (E) <br/> Local s ={}-- a stack <br/> Local a, B, c <br/> For I, V in ipairs (E) DO <br/> If type (v) = "Number" then <br/> table. insert (S, v) <br/> elseif type (V) = "string" then <br/> If # S <2 then <br/> error (not enough values ') <br /> End <br/> B = table. remove (s) <br/> A = table. remove (s) <br/> table. insert (S ,'('.. A .. V .. B .. ') <br/> end <br/> If # S ~ = 1 then <br/> return nil, 'Not ugh operators '<br/> end <br/> return s [1] <br/> end </P> <p> -- the first four columns indicate four numbers in the position in the reverse Polish formula, the last three columns indicate the positions of the three operators in the inverse Polish formula <br/> -- this table is calculated by hand <br/> Local rpn_patterns ={< br/> {1, 2, 4, 6, 3, 5, 7}, <br/> {1, 2, 4, 5, 3, 6, 7}, <br/> {1, 2, 3, 6, 4, 5, 7}, <br/> {1, 2, 3, 5, 4, 6, 7}, <br/> {1, 2, 3, 4, 5, 6, 7} <br/>}</P> <p> Local ar ={}< br/> Io. write ('Please input 4 numbers: ') <br/> Fo R I = 1, 4 DO <br/> ar [I] = Io. read ('* n') <br/> end </P> <p> local operators = {' + ','-','*', '/'} </P> <p> -- inverse Polish formula <br/> Local exp ={}</P> <p> for a = 1, 4 DO <br/> for B = 1, 4 DO <br/> If B ~ = A then <br/> for C = 1, 4 DO <br/> If C ~ = A and C ~ = B then <br/> for D = 1, 4 DO <br/> If D ~ = A and D ~ = B and d ~ = C then <br/> for I = 1, 4 DO <br/> for j = 1, 4 DO <br/> for k = 1, 4 DO <br/> for M = 1, 5 DO <br/> exp [rpn_patterns [m] [1] = ar [a] <br/> exp [rpn_patterns [m] [2] = ar [B] <br/> exp [rpn_patterns [m] [3] = ar [c] <br/> exp [rpn_patterns [m] [4] = ar [d] <br /> exp [rpn_patterns [m] [5] = operators [I] <br/> exp [rpn_patterns [m] [6] = operators [J] <br/> exp [rpn_patterns [m] [7] = operators [k] <br/> Local T = evalpo (exp) <br/> If t = 24 then <br/> Io. write (rpntoexp (exp), '/N') <br/> OS. exit () <br/> end <br/> end <br/> Print ('maze24 has no idea. ')

If you don't have a Lua interpreter on your computer, you can also use codepad (http://codepad.org/) to run this code online. But codepad cannot be entered, so you need to slightly modify the code:

 

Remove the for loop next to 'Please input 4 numbers: 'and change it:

AR = {5, 5, 5, 1}

Or fill in four other numbers that you want to add up to 24.

 

PS. By the way, we will attach the following three groups of collected questions:

 

1 4 5 6
6 7-6 2 // It's still a negative number. Let's go.
4 4 10 10

 

If you cannot figure it out, use the following program to check the answer: P

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.