[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
|
|
Problem DescriptionInsert + 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 & TechniquesLike 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
Suggestions for Further ExplorationsA few possibilities:
|
[Feedback] [Newsletters (subscribe/view)] [About me]Copyright © 2000-2018, Gary Darby All rights reserved. |