A three-digit number has two properties. The tens-digit and the ones-digit add up to 5. If the number is written with the digits in the reverse order, and then subtracted from the original number, the result is 792. Use a system of equations to find all of the three-digit numbers with these properties.
Let’s assume that a three-digit number is defined where x = 100s place, y = 10s place and z = 1s place. This means that the first property: y + z = 5 and the second property: (100x + 10y + z) - (100z + 10y + x) = 792
Reducing the second property we get: 99x - 99z = 792.
The set of linear equations are then: y + z = 5 99x - 99z = 792
Let’s check to see what values work between both equations by setting y to different values.
y <- seq(0,9)
df <- data.frame(y)
df <-
df |>
mutate(z = 5 - y)
knitr::kable(df, row.names = FALSE)
y | z |
---|---|
0 | 5 |
1 | 4 |
2 | 3 |
3 | 2 |
4 | 1 |
5 | 0 |
6 | -1 |
7 | -2 |
8 | -3 |
9 | -4 |
We know that z can’t be negative as that would not make sense having a negative sign between digits on a number and is not in one of the two mentioned properties. So let’s now focus on where y >= 0 and y <= 5 in the second equation
df <- df[0:6, ]
df <-
df |>
mutate(x = (792 + (99 * z)) / 99) |>
select(x, y, z)
knitr::kable(df, row.names = FALSE)
x | y | z |
---|---|---|
13 | 0 | 5 |
12 | 1 | 4 |
11 | 2 | 3 |
10 | 3 | 2 |
9 | 4 | 1 |
8 | 5 | 0 |
Since a digit can only be from 0 to 9, we can then remove answers where x >= 10. This leaves the last two rows in our table.
knitr::kable(tail(df, 2), row.names = FALSE)
x | y | z |
---|---|---|
9 | 4 | 1 |
8 | 5 | 0 |
We end up with two solutions. Let’s confirm this.
Property 1: y + z = 5
y <- 4
z <- 1
y+z
## [1] 5
Property 2: 99x - 99z = 792
x <- 9
z <- 1
99*x - 99*z
## [1] 792
Property 1: y + z = 5
y <- 5
z <- 0
y+z
## [1] 5
Property 2: 99x - 99z = 792
x <- 8
z <- 0
99*x - 99*z
## [1] 792
There’s probably an easier way to solve this, but I tried it based on what I would have done with pencil and paper. It also happens that the properties gave only two solutions for a three digit number. This would become much more complicated as the number becomes larger.