## DEV Community

Brian Kirkpatrick

Posted on • Originally published at github.com

# [10/52] Spicy Math!!!

Fun tangent today--some spicy math! We're going to look at some fundamental problem-solving skills that have a strong relationship to the engineering design process.

## Background

My daughter send me some interesting problems from school from a website that lets you build specific challenges and math puzzles:

https://solveme.edc.org/whoami/

But we're not just talking about math puzzles here! There are some important concepts in logic and problem-solving here that have really big ramifications. Here's the first of two problems she sent:

So you can see there are three digits in a number we need to find out: `t`, `u`, and `d`. Technically these are "tens", "units", and "decimal" (tenths) places. But, we can pretend they are separate variables simply because they're "things that we don't know". Same thing! The objective is to find out what this number is given these clues.

Here's another problem in a similar form.

These problems come from this website that, as it turns out, has a variety of problems across different difficulty levels. You can can even build your own--which is likely where these came from, having been generated by the community to solve. These can be straightforward, or they can be pretty difficult--but the structure of the problem is really interesting here, so let's take a deeper look at this.

## Basic Example

Let's do some doodling and figure out how this is supposed to work.

We have two digits in a number: `t` and `u`. We'll pretend these are variables, because all a variable is, is something you don't know. We don't know `t`, and we don't know `u`.

Then, we have two clues. The first clue is, "all the digits are even". In this case, that means we know:

• `t` is even

• `u` is even

The sum of the digits is equal to ten, so we also know:

• `t + u = 10`

And we know the tens digit is equal to six more than the units digit:

• `t = 6 + u`

So, we have more than enough to solve this problem right now, but first I want to talk a little bit about how these problems are set up and structured.

## Variables

What we have here, as `t` and `u`, can be called "variables". We have two of them, and they can go by a couple of different names, including "unknowns" (can you guess why!?), or if you want to get really fancy (especially with more sophisticated problems in engineering) we may call these "degrees of FREEDOM". All of these terms refer to the same stuff, though.

The quintessential idea here is, there's something you don't know YET, and you want to figure out what it is.

So, we have two things we don't know, and we're going to use the letters `t` and `u` to represent those two things.

## Constraints

On the other hand, we have the bullet points from above. Problems are defined by how many of these, and how many variables, there are. Let's say the number of constraints is `m`, and the number of variables is `n`. In this case, `n = 2` because we have two things we are trying to figure out.

From the bullet points above, we have what are called "constraints". They can also go by a couple of different names; an "equation" is a kind of constraint, and if you think about it the `t is even` is a kind of constraint too (although it's more about what KIND of possibilities you are constraining the variables to, as opposed to equations where you define a specific relationship. "Relationships" or "memberships", in this manner, can also be considered kinds of constraints.

For purposes of this problem, though, we have a certain number of equations, so we'll say the number of constraints is `m = 2`.

## Problems

You can talk about problems in terms of how many "variables" and how many "constraints" there are.

As it turns out, how many variables and how many constraints there are tell you A LOT about a particular problem. So, let's consider our first basic problem from above and see if we can figure it out.

(As an aside: As it turns out, the "is even" constraints don't matter. We only need 2 constraints to solve the problem, so we'll ignore these for now.)

So, in this case we'll observe that `t` is equal to `u + 6`, so we'll take that "definition" of `t` and plug it into the first equation. Replacing `t` with `u + 6` creates a new equation with just `u`:

`u + 6 + u = 10`

All we've done is replace one `t` with another definition of `t`. Now we have a specific equation with just `u`, so we can do some math to move numbers around and figure out the value of `u`. First, we'll subtract `6` from both sides:

`u + u = 4`

And now, we can also express `u + u` as `2u`:

`2u = 4`

Now we can divide both sides by `2`:

`u = 2`

And we have a value for `u`! So, `u` is no longer an unknown! We have a value for that variable, which is pretty cool. Not only that, we can now take that and plug the value of `u` into the equations to figure out what `t` is. So, using the second equation, we'll replace `u` with `2`:

`t = 2 + 6`

Obviously, this is just a little addition:

`t = 8`

So here's our solution!

`u = 2`

`t = 8`

## But Why?

We were able to get a solution here. Notice that we didn't even need the "is even" constraints (though they turned out to be true anyway). The two equations were enough to figure out a unique specific solution.

So, we had a certain number of constraints (`n = 2`) and a certain number of variables (`m = 2`).

And now for the crazy part:

YOU CAN COME UP WITH A UNIQUE SOLUTION FOR A PROBLEM IF THE NUMBER OF CONSTRAINTS IS EQUAL TO THE NUMBER OF VARIABLES

Or, in math speak, if:

`n = m`

So, you can back out unique values for your variables (or unknowns) if you have the same number of equations, or constraints.

• If you have 2 variables, you need 2 constraints to find a unique solution

• If you have 3 variables, you need 3 constraints to find a unique solution

...etc.

## Back to the Other Problem

Let's consider one of the "original" problems--the one with `t`, `u` and `d`. So, we're trying to find a number represented by `tu.d`. We have some clues here (another name for constraints):

The first clue is:

`90 = 14`

And the second clue is:

`17 != 135e3424626e3`

You can tell, this is probably a community-generated problem! There's a couple of things you'll notice:

First off, the first constraint is a contradiction.

`90 = 14`

A contradiction cannot be true! 90 is equal to 90, and 14 is equal to 14, but 90 is not equal to 14. This constraint (this relationship or constraint) is a contradiction.

But not only is it a contradiction--`t` does not show up in this constraint, `u` does not show up in this constraint, `d` does not show up in this constraint. So, this constraint is completely irrelevant to our problem!

Now let's look at the second one: `17` is not equal to... blah blah blah blah blah. We have no idea what this is supposed to represent! We have a number on the left-hand side (`17`) and an inequality, which is technically true--this is indeed NOT equal to the right-hand side! But even if there is a digit value for `e` that is supposed to be helping "build" a number on the right-hand side, there's still no relevance to the variables in our problem (`t`, `u`, and `d`).

So how many constraints do we have?

`n constraints = 0`

And how many variables do we have?

`m variables = 3`

## n and m

In this case `n < m`--in other words, we have more variables than we do constraints.

• When `n = m` (when the number of constraints is equal to the number of variables), we're good! We can find a unique solution, and we're happy.

• When `n > m` (when the number of constraints is greater than the number of variables), sometimes we can still find a solution. But often what you'll see is, there will be multiple constraints that cannot simultaneously be true. Sometimes we call this "over-constrained". And if you're not careful with an "over-constrained" problem what you'll find is that this leads to contradictions. So, instead of having ONE unique solution, there will be ZERO solutions to the problem--there will be no values for your variables that can satisfy all of the constraints.

• What about the opposite? When `n < m` (when the number of constraints is less than the number of variables), we don't have enough ways to "pin down" the possible values of those variables. In this case, we call this "under-constrained".

So, there's a balancing act here. It has to be just right. There have to be the same number of constraints as there are variables, if we want to be able to find a unique solutions.

If it's under-constrained instead, we'll have too many possible solutions! Sometimes you can find a way to express the family of solutions that there might be--maybe it's the family of solutions on a line, for example (if this variable has this value, than that variable has to have that value). But there won't be one unique solution to the problem; there will be many solutions.

So, these are the three kinds of problems in life--in math, in logic, in science, if you're designing an experiment, if you're trying to figure out what a relationship is, anything. If you're designing something in engineering (how long does the bridge have to be? how tall does the bridge have to be?), too! This is a very powerful idea, as it turns out.

So, we want the number of constraints to be equal to the number of variables--then we know there should be a unique solution we are trying to find.

In the first example, then, we KNOW there are infinitely many solutions, because the problem is severely under-constrained.

## The Other Problem

This problem introduces some interesting ideas with respect to something called "sets". Let's break this down a little bit:

`k`, `h`, `u`, `d`, `t`, `e`, and `m` are our variables.

How many variables do we have? Seven--so, `m = 7`.

How many constraints do we have? Go ahead and count them. There are seven! So far, so good.

Now let's write down what those constraints are.

First, you'll see

`k != ...`

In other words, all of these variables are unique--none of these digits are the same. But there's also an implicit constraint that we haven't talked about. `u` cannot be equal to `-3`, for example--these are all digits, they have to be part of a number. `u` can't be equal to 2.5, or 6000, or pi. We'll use "set notation" here to indicate all of these variables, using `k` as an example, have to `EXIST` within a certain set of values. We'll use an `E` to represent this (though in set notation, and the example shown above, it's more like a backwards lower-case `e`):

`k E {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}`

These possible values are non-negative integers less than 10. (Integers are whole numbers like `-2`, `-1`, `0`, `1`, `2`, etc.) In this case, these values are all of the "non-negative" integers, greater than or equal to 0, and less than 10. But this is just a fancy way to define, or say, that all of these digits must exist within that set.

That's another constraint! It's another constraint that is implicit to the structure of the problem. So, now we have our first constraint ("the digits must be unique"), and our second constraint ("the digits must be non-negative integers less than 10").

Let's consider the next constraint:

`A x E {k...}, 2 | _`

This `A` means (though it is upside-down in the set notation and screenshot example) "for All values of". This `A x E` tells us that "for all values of x that exist within the following set". So far, so good.

But `2 | _` is nonsense. It doesn't mean anything. So, someone created this problem, but they didn't create clues that make sense. "For all values of x (for all digits), `2 | _`" doesn't mean anything. So, we can't use this one! It's not a constraint we can use, and it's not relevant to our problem. And the following constraint is basically the same.

Let's consider the one after that, though. (That is, the fourth line.) This one is actually useful!

`A x E {k...}, _ is prime`

This is pretty straightforward! And now we know how to interpret this: "for all digits, each digit is prime". We are assuming that `x` and `_` are the same thing, but that seems reasonable. In other words, we have a third constraint: All of the digits are prime. This is great! This is a clue. This helps us constrain, or narrow down, the possibilities. So, let's look at the possible values of all of our digits.

We have at least three constraints here so far:

• We know they're all unique

• We know they're all digits

• We know they're all prime

What does that tell us? Let's see what values our digits can possible have.

• 2, 3, 5, and 7 are prime

• 0, 1, 4, 6, 8, and 9 are not prime

In other words, there are only four values for our digits that satisfy this constraint!

We have a problem. What is it?

We have three constraints so far:

• The digits have to be unique

• The digits have to be digits

• All of the digits are prime

But we have SEVEN variables we need to find digits for! So, not ALL of these constraints can be true at the same time. If ALL of the digits are prime, there are only four possible values, so they cannot all be unique! So, not all three constraints can be satisfied.

Because we have a contradiction within our constraints, we know that this problem does not have any solutions.

We could go through these other constraints:

• "For all digits, they are square numbers": in a similar way, possible values here only include `0` (zero squared), `1` (one squared), `4` (two squared), and `9` (three squared)--only four possible values again. We know they can't all be unique, they can't all be digits, and they can't all be squares at the same time. Not all of these constraints can be satisfied either!

• Triangular numbers (let's assume this means "cubed") have a similar problem. Possible values include `0` (zero cubed), `1` (one cubed), and `8` (two cubed). Only three values! Can't be true.

• "...is integers" is true inherently, because we've defined these values to be digits, and all ten possible digits are integers. This constraint is fine, and a natural outcome of the fact that these variables are all digits, but it doesn't add any new information, so it doesn't count as an additional constraint.

We don't have enough constraints that don't also cause contradictions!

So, unfortunately, this problem has `0` solutions. The contradictions, in effect, indicate that the problem is "over-constrained". There aren't any solutions that satisfy all of these constraints.

## In Summary

So, BOTH of these problems cannot be solved. We can't come up with unique values for these digits (or variables) that satisfy all of these constraints at the same time.

Here are some other examples:

In this case, if the number is between 1 and 2 (exclusive), than we know the units must be 1. But if `u = 1`, then it cannot be a multiple of 2! Another contradiction. Our problem is "over-constrained".

Here's another one:

We don't even need to look at the problem itself. There are two variables and only one constraint (which, by the way, doesn't tell us anything about our problem.) So, this problem is "under-constrained".

Let's find a more tractable problem. There are considerably more complicated problems here (not from the community, but from higher levels of difficulty). Let's choose an official one:

We have 4 variables: `k`, `h`, `t`, and `u`. What are our constraints?

• `khtu`, the number that's formed when you put all of them together, is odd. In other words, `khtu` exists within the set of all odd values. But this doesn't constrain `k`, `h`, or `t`. But it DOES constrain `u`, because `u` is the digit that determines whether or not the number is odd or even. In other words, `u E {1, 3, 5, 7, 9}`. We have one constraint!

• The sum of the digits is equal to 16. In other words, `k + h + t + u = 16`. We have two constraints!

• The tens digit is prime. In other words, `t E {2, 3, 5, 7}`.

• The product of the digits is equal to 80. In other words, `k * h * t * u = 80`.

• The thousands digit is a square number. In other words, `k E {0, 1, 4, 9}`.

So, we have some "quasi-constraints" here. We know what "possible" sets of values there are, and we know how they are related. What we can do now is, "guess and check". We know it has to be one of these, so let's "choose" one of these possibilities and see what happens. "What if", in other words, "t was equal to 2?"

`t = 2`

Then we have two constraints with three variables:

`k + h + 2 + u = 16`

`k * h * 2 * u = 80`

Now let's consider, "what if k is equal to 0?"

But if `k = 0`, then `k * h * t * u` would not be equal to 80! If `k = 0`, the product will be `0`, no matter what the other values are. So, we're narrowing it down! We're "searching" these possible values.

What if `k = 1`? Then we'd have:

`1 + h + 2 + u = 16`, or in other words, `h + u = 13`, and:

`1 * h * 2 * u = 80`, or in other words, `h * u = 40`.

Can `u = 1`? No, because then `h = 12` and `12 * 1` is not equal to 40.

Can `u = 3`? No, because then `h = 10` and `10 * 3` is not equal to 40.

Can `u = 5`? Ah ha! That would give `h = 8`. And `8 + 5` is equal to 40! We've found a solution. How cool is that?

This is A solution, though. We haven't exhaustively looked at ALL the possible solutions. We've just hypothesized "what if k is equal to this", "what if u is equal to this", etc. This does not guarantee a unique solution, because we only have two explicit constraints. But this is A solution. So what is our number?

`k, h, t, u = 1, 8, 2, 5`

In other words, `1825` satisfies all of these constraints! It just might not be a unique solution. Let's plug that in and see if it likes that solution:

Hey, that worked! Cool beans.

## In Summary

If there's a concept you should take away from this fire-hose, it's this:

• Any given problem will have `n` constraints (preferably equations, since we can manipulate those with the power of algebra!) and `m` variables

• If `n = m`, we're happy! There should be a unique solution to the problem.

• If `n > m`, we are "over-constrained" and there may be contradictions that indicate there are no solutions.

• If `n < m`, we are "under-constrained" and there may be more than one solution to the problem

And now you know!