[Home] [Puzzles & Projects] [Delphi Techniques] [Math Topics] [Library] [Utilities]
Write a program which will resolve problem text and present the solution for this problem (and other similar "digit position" problems):
Find a six-digit number in which the first digit is two
less than the fifth, the second digit is one more than the fourth, and the fifth
digit is four less than the last. The sum of the first and fourth digits equals
the second, the sum of the third and last digits equals the second, and the sum
of all the digits is 30."
Background & Techniques
This is another experiment in writing a program to convert a certain type of
story problem to algebraic equations. A previous "Age
Problem Solver" program solves
problems describing the relationship between the ages of two people. The current
project applies similar techniques to solving "digit relationship" problems like
the one above from my current "Mensa Puzzle-A-Day" calendar:
There are some syntax/vocabulary problems which are solved by hard coding
rather than via table entries.
Unresolved parsing problems
There are some unresolved parsing problems. One is in identifying the
beginning and ending of phrases which define equations. The program currently
uses comma or decimal point (, or
.) as reliable separators, but humans frequently
omit the comma. So a phrase like "The first is the sum of the second and the
third and the fourth equals the fifth" will not be correctly parsed unless the
second "and" is preceded by a comma. We probably need code to look for
multiple equivalence indicator words (is, are, equals) in what is initially
identified as a sentence, and then try to identify where it should be split. Another problem is that, at least in English, we use position words like
"third" and "fourth" to indicate both position and as denominators in fractions.
So a phrase like "The fourth digit is one fourth of the fifth" is much harder
for a computer to understand than it is for a human.
From a programming point of view, there is nothing particularly clever or innovative about the code. I had originally implemented our Gaussian Elimination class to solve N equations in N unknowns, but it turns out that problems about N digit numbers may have more or fewer than N equations. As a result, I reverted to the same "Brute Force", exhaustive search procedure used in the Age Problems program. It uses our TEval class which takes an expression and an array of variable names & values as input, and returns the value of the expression when those variable values are plugged into the expression. By assigning the N position variables to all feasible integers (10,000 to 99,999 for 5 digit numbers, 100,000 to 999,999 for 6 digit numbers), we can find one or all solutions to the set of equations by evaluating the left and right sides and comparing the results for equality.
Suggestions for Further Explorations
Copyright © 2000-2014, Gary Darby
All rights reserved.