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:
- What is the sum of the two digits of your integer?
- What is the difference between the first digit and second digit of your integer?
As an example consider the integer 36.
- What is the sum of the two digits of your integer?
3 + 6 = 9 - 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:
- What is the sum of the two digits of your integer?x + y = s
- What is the difference between the first and second digits?x − y = d
We can solve these two equations for x and y. Starting with equation #1, we solve for y and get equation #3:
- x + y = s
- y = s − x
Substituting y from equation #3 into equation #2, and solving for x, we get equation #4:
- x − y = d
- x − (s − x) = d
- x − s + x = d
- 2x = s + d
- x =s + d2
Substituting x from equation #4 into equation #1 and solving for y, we get equation #5:
- x + y = s
- + y = ss + d2
- s + d + 2y = 2s
- 2y = 2s − s − d
- 2y = s − d
- y =s − d2
Example 1
Now we can use equations #4 and #5 to easily and quickly calculate a person’s integer. Again consider the integer 36.
- What is the sum of the two digits of your integer? 9
- 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:
- x ==s + d2=9 + −32= 362
- y ==s − d2=9 − −32= 6122
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.
- What is the sum of the two digits of your integer? 6
- 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:
- x ==s + d2=6 + 42= 5102
- y ==s − d2=6 − 42= 122
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}.")