Four Fours (and more)

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

 

 

Search

Search WWW

Search DelphiForFun.org

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.)

Contact

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

Search DelphiForFun.org only

 

 

 

Problem Description

Here's a puzzle from "Mathematical Bafflers", Angela Dunn, Dover Books::

Using mathematical symbols to modify four fours it is possible to write expressions for all the numbers from 0 to 100. 

Background & Techniques

The original problem as I interpreted it, was to find an expressions containing only the four digits 4444, parentheses, the operators {+, -, *, /, and ^} (Plus, minus, multiply, divide, and exponential), which evaluate to all values from 0 to 100.

It turns out that there is no solution under these conditions.  Angela Dunn's  solutions was less than satisfactory to me because it includes other tricks such as allowing the symbol "!" for factorial, √,  the square root symbol, and decimal points. These all imply using digits not specifically included, specifically:  all smaller digits for factorial, "2" for square root, and divide by 10 for the decimal point). The problem  did however lead me to generalize the problem to allow selection of other expression digits, operators, and targets.

The program evaluates all possible arrangements of the operators being used with repeats allowed for all possible ways to parenthesize a sting of alternating values and operators.

 Using my rules, there is no set of 4 digit values which  generate all values from 0 to 100, but 9252 will
generate 75 of them.  Using 5 digits, at least on set , 13792, in that order can  generate all 101 target values.  .

A couple of notes: The program allows an implied "concatenation" operator so any adjacent digits will be tested as if they were a single integer.  All operations are integer based, therefore the divide operator, "/", represents integer division and remainders are dropped ( "1 / 2" returns 0, "10 / 3" returns 3, etc).
 

Here are a few of the program's features:

bulletCheck boxes select which operators are used in forming expressions.
bulletA variable number of  user specified digits can be used to form expressions
bulletAn option allows input digits to be permuted to generate additional expressions.
bulletA range of target values may be entered.
bulletDisplay options show all expression values which match a target value, or only the first expression which matches.

Notes for Programmers

I enjoy writing programs which stretch the mind solve problems which have  no particular need to be solved. This is one such.   It led me back to  a number of previously written DFF programs and units including    the Catalan Numbers program to create a sharable unit, UGetParens with the GetParens function  which will generate all possible ways to parenthesize a given  expression.  I also created UTEvalInt unit defining the TEvalInt  class, an integer arithmetic version of our TEval expression evaluator class.  The old reliable UCombosV2 library unit provided the TCombo class used to permute the input integers when the users requested that option. 

I believe that TCombo could also provide the "multiset" permutations we need for the operators  .But "just for fun", I decided to use a "counting in Base N" method which used previously other project.    For creating arrangements of operators when repeats are allowed, an easy way is to create numbers of base N to permute N operators.  If we need M operators, we'll create all M digit numbers of base N. There are NM of them.  As a simple example, assume we want to use only the plus and minus operators to insert into the three gaps in a set of  4 variables or constants, (a+b+-c+d, a+b+c-d, etc.) .  In this case N=2 and M=3.  The 8 (23 ) base 2 numbers (000, 001, 010011, 100, 101, 110, 111) represent all possible ways of inserting 3 + and - symbols into a set of 4 variables.

As a more complex example, suppose we are using  the 4 operators {+, -, *,  /}  (N=4)  and we need sets of 3 operators (M=3) to insert into a set of 4 variables to form expressions of the form "a op b op c op d".  Three digit base 4 numbers can map 3 of the 4 operators into the three "op" positions.  Our base 4  numbers will look like {000,001, 002, 003, 010, 011, ...320, 321, 322, 333}    Since there are 4 choices for each of 3 positions, we have  4x4x4=64 numbers (or arrangements).  If we assign each base 4 digit to an operator, we'll have the 64 possible  ways to insert operators between the digits to for our expressions.  If  we map digits  {0,1,2,3}  to  {+. -, *, / }  then, for example,  the value 320 generates "4 / 4 * 4 + 4".    

Running/Exploring the Program 

bullet Download source
bullet Onetime download of Library unit DFFVLIB13 or later  required to recompile source code
bulletDownload  executable

Suggestions for Further Explorations

Expressions contain redundant parentheses which could be removed for improved readability.. .

 

Original:  November 29, 2011

Modified:  May 15, 2018

  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2018, Gary Darby    All rights reserved.