Number Guessing Game

Very often we study mathematics but fail to see its application in life. As a simple example consider the experience of one of my students. He decided to write a computer program that would ask a user to think of a integer between 0 and 99, then ask the user two questions about the digits of the integer, and then guess the integer. The two questions about the digits of the integer were:

  1. What is the sum of the two digits of your integer?
  2. What is the difference between the first digit and second digit of your integer?

As an example consider the integer 36.

  1. What is the sum of the two digits of your integer?
    3 + 6 = 9
  2. What is the difference between the first and second digits?
    3 − 6 = −3

My student wrote a program that contained 100 if statements similar to this:

if (sum == 0 && diff == 0) {
    number = 0;
}
else if (sum == 1 && diff == -1) {
    number = 1;
}
else if (sum == 2 && diff == -2) {
    number = 2;
}
 ⋮

Of course, it is not necessary to write 100 if statements to solve this problem. In fact, by asking the user the two questions, the student created a set of two algebraic equations with two unknowns but didn’t realize it. We can solve this system of two equations algebraically and write a much shorter program to guess the user’s integer. Let’s write the two equations.

Equations

Consider any integer between 0 and 99 inclusive. Any integer in this range will have two digits. We will call the first digit (the tens digit) x and the second digit (the ones digit) y. Each of the two questions leads to an equation:

  1. What is the sum of the two digits of your integer?
    x + y = s
  2. What is the difference between the first and second digits?
    xy = d

We can solve these two equations for x and y. Starting with equation #1, we solve for y and get equation #3:

  1. x + y = s
  2. y = sx

Substituting y from equation #3 into equation #2, and solving for x, we get equation #4:

  1. xy = d
  2. x − (sx) = d
  3. xs + x = d
  4. 2x = s + d
  5. x =
    s + d
    2

Substituting x from equation #4 into equation #1 and solving for y, we get equation #5:

  1. x + y = s
  2. s + d
    2
    + y = s
  3. s + d + 2y = 2s
  4. 2y = 2ssd
  5. 2y = sd
  6. y =
    sd
    2

Example 1

Now we can use equations #4 and #5 to easily and quickly calculate a person’s integer. Again consider the integer 36.

  1. What is the sum of the two digits of your integer? 9
  2. What is the difference between the first and second digits? −3

We substitute 9 and −3 into equations #4 and #5 for s and d:

  1. x =
    s + d
    2
    =
    9 + −3
    2
    =
    6
    2
    = 3
  2. y =
    sd
    2
    =
    9 − −3
    2
    =
    12
    2
    = 6

We calculated that x is 3 and y is 6, so we know the integer chosen by the user was 36.

Example 2

Let’s try another integer, 51.

  1. What is the sum of the two digits of your integer? 6
  2. What is the difference between the first and second digits? 4

We substitute 6 and 4 into equations #4 and #5 for s and d:

  1. x =
    s + d
    2
    =
    6 + 4
    2
    =
    10
    2
    = 5
  2. y =
    sd
    2
    =
    6 − 4
    2
    =
    2
    2
    = 1

We calculated that x is 5 and y is 1, so we know the integer chosen by the user was 51.

Python Program

Here is a simple python program that uses equations #5 and #4 as part of a number guessing game. Notice that the code to calculate the integer requires only three lines instead of 100 if statements.

#!/bin/python3

print("Think of a integer between 0 and 99.")
print("I will ask you two questions about your")
print("integer, and then guess your integer.")
print()

# Get the sum of the two digits from the user.
print("Question #1")
s = int(input("What is the sum of the"
        " two digits of your integer? "))
print()

# Get the difference of the two digits from the user.
print("Question #2")
d = int(input("What is the difference between the\n"
        "first and second digits of your integer? "))
print()

# Calculate the integer chosen by the user.
x = (s + d) / 2
y = (s - d) / 2
n = int(x * 10 + y)

# Use an f-string to print the user's
# integer for the user to see.
print(f"Your integer is {n}.")