Expression = 100

[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

Insert + and - signs as necessary in the string 123456789 to form an expression that evaluates to 100.  For example:  12+3-4-5+6+78+9=99 (Darn, so close!)

Background & Techniques

 Like many puzzles, developing a strategy for solving is half the battle.  I decided to treat the 8 spaces between the digits as "buckets" into which we'll put either  '+', '-', or nothing.  So we have 3 choices for 8 positions. That's only 38 possible solutions, somewhere around 700. So a brute force approach seems feasible.  

If we generate base 3 numbers from 00000000 to 22222222, we should cover all possibilities.   There's a simple base converter function included here that converts integers to any base.  

The rest was just some debugging to get the test calculations to evaluate correctly and some display code to show solutions when the result of a trial matches the target.  Inserting the operations turned out to be a lot like the SimpleCalc program - we need to defer performing an operation until we gather the second operand. 

We might as well solve for any number input by the user, since the computer doesn't really care about the value it's comparing  against.  

Addendum May 31, 2002:  A viewer pointed out that one additional expression totaling 100 can be generated if a leading minus sign is allowed.  I changed the program to generate 39 values and  processed only the "no sign" and '-' values for the initial position.  (Otherwise,  leading '+' and leading "no sign" generate duplicate solutions --  discovered the hard way.)  Expressions100 has been replaced with Expressions100B to implement this "feature".

  Running/Exploring the Program 

bulletBrowse source code extract
bulletDownload Source
bulletDownload executable 

Suggestions for Further Explorations

A few  possibilities: 

What is the smallest value for which there is no solution?  
Which value has the most solutions?
What if we added * and / to the operation choices? 
Add 0 to the end of the digit string.  
Original: November 11, 2000

Modified: May 15, 2018

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