Home  |  Introduction  |  Newsletters  |  Programs  |  Feedback

 

 

Search:

WWW

DelphiForFun.org

Support DFF

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

If you shop at Amazon anyway,  consider using this link. 

     

We receive a few cents from each purchase.  Thanks

 

 

The index page for all Programs on the site?

The DFF Play CD?

Zipped file DFF Play CD.zip contains  executable version of about  75 of the 200+ programs from the site, mostly those I particularly liked or thought would be of widest interest for non-programmers.  The file is rather large, about 20mb..

Anything else?

 

Google
 

 

    

Search WWW

Search DelphiForFun.org

 

Not a programmer (yet)?

 That's OK -  the executable version for any puzzle or  game you find here is available for download.  Just scroll down to the bottom of most any description page and you'll find a "Download executable" link. Downloaded programs are in "zipped" format to reduce size and may require an "unzipper" program unless you are running Win XP or later.  Here's a link to a free one. 

Check  out  the Most Popular  Downloads from DFF   (updated weekly)

First time visitor?

Take a look at the Introduction page to see what this site is about

Notes for Teachers

 

 

Problem Description

Here's a nice little puzzle:

Consider the equation below, where "op" represents one of the four basic operators + (addition),  - (subtraction),  * (multiplication) or / (division).

X = 9 op 3 op 2 op 1

Complete the equation by filling in the "op" operators to produce X values of 0 through 9 in as many ways as possible.   For example, 4 = 9 / 3 + 2 - 1.

Source:: Adapted from an educational  newsgroup posting by Kaidy Educational Resources

Background & Techniques

Since we are working with three operator locations, each with four possible values, there are only 4X4X4=64 possible arrangements.  Hardly worth implementing a full scale expression parser and evaluator.   (although  the Scientific Graphic Calculator project has one that would do nicely).

Instead, let's start by looking at the order of operations required to evaluate the expression, '*' and ' /'  must be performed before '+' and   '-'.    We'll call '*' and '/'  high priority and '+' and '-' low priority  operations.  There are only possible 8 sequences  of high and low priority operations  (low, low, low),  (low, low, high),  (low high, low), (low, high, high), (high, low, low), (high, low, high), (high, high, low), and (high, high, high).   So 9+2*3-1, for example belongs to the (low, high, low) category.    If  we can determine which of these 8 patterns applies to a particular expression, then three "If" tests for each pattern, one for each operator,  performed with high priority first, should be enough to distinguish '*' and   '/'  from '+' and  '-'.  Function Eval does this.    

Now we have to figure out how to pass all 64 of the possible expressions to Eval.  We'll just arbitrarily assign values 1,2,3 and 4 to operations +, -, *, and / respectively.   Procedure ShowValues uses a  triply nested loop to do this.    Since we're generating all 64 solutions but only want those that evaluate to 0 through 9,   ShowValues  takes  high and low limits as parameters.    Given that, we might as well add a button to show all values - just in case someone wonders  about the highest possible value. for example.    ShowValues adds  formatted versions of the equations to a stringlist - we'll put the result values first so that we can easily sort  equations before displaying them.  

Notice too that 16 of the 64 values do not produce integer expression at all - namely those with  3 / 2  as the middle operation.   I return  -9999 from Eval  as a special flag value to indicate this case.      

Running/Exploring the Program 

Suggestions for Further Explorations

It shouldn't be too difficult to take a different set of values instead of 9,3,2,1.  Surely there's another set that would work as well?  
Is there a set that would produce more solutions?
In the current case, any non integer result from a single operation guarantees a non-integer result.  With other numbers that would not necessarily be true.    Program needs to specify whether a non-integer term disqualifies the expression, or only non-integer expression values.