The 9321 Problem

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]




Search WWW


As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.


Support DFF - Shop

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


We receive a few cents from each purchase.  Thanks


Support DFF - Donate

 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.

Mensa Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa 365 Puzzlers  Calendar 2017

Mensa 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)


Feedback:  Send an e-mail with your comments about this program (or anything else).

Search only





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 

bulletBrowse source extract
bulletDownload source
bulletDownload  executable

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.
  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2018, Gary Darby    All rights reserved.