1. Factoring a Quadratic
Chapter 3 consists of puzzles that were inspired by the web.
2. What's the probability that a quadratic will factor?
In this first puzzle our question is, given random integers a, b, and c in a quadratic equation, what is the probability that the quadratic will factor? This was posed by Bob Lochel, a math and statistics teacher at a high school outside of Philadelphia, who runs a blog called mathcoachblog.
3. Factoring a quadratic
Consider the expression x squared plus 3 x plus 2.
In this case, a is 1, b is 3, and c is 2, representing the numeric coefficients attached to x squared, x, and the constant, respectively.
Using elementary algebra, we can factor this quadratic into the quantity x plus 2 times x plus 1. So, this quadratic is factorable.
How would we write code to check whether any given quadratic is factorable?
4. Quadratic formula
The answer lies in the quadratic formula, which gives the solutions to the equation where the quadratic is set equal to 0.
We define a quadratic as factorable if the solutions are rational numbers, meaning that they can be expressed as the ratio of two integers.
First, we note the quantity known as the discriminant, which is the expression within the square root, b squared minus 4 times a c. If the discriminant is negative, the solutions are imaginary and therefore not rational.
5. Using the discriminant
We can determine whether the discriminant is negative as our first check, using an if statement as shown. We can also code this with variable names instead of specific numeric values.
6. Is it a perfect square?
Next, we note that the solutions will be rational if and only if the discriminant is a perfect square, meaning that it can be expressed as the product of a whole number with itself.
To check whether the discriminant is a perfect square, we can use the round function, which rounds its input to the nearest integer. Thus, we can check whether the rounded value is equal to the original value. If they are equal, the original value is an integer, and if not, the original value is not an integer and the equality check will return FALSE. Here, we run this check on the square root of the discriminant from our example quadratic equation.
Note that R has a function called is dot integer, whose name suggests that it can accomplish this for us as well. However, this function is checking for the internal representation of the value, which is not of type integer by default, so we cannot use this function here.
7. The else conditional
We can also specify through code that a section of code should only be performed if a previous condition was false.
Consider the following code, where we evaluate the square root of a value only after it is confirmed that the value is positive. We do this using the else conditional, as shown here.
If we did not first check whether the value was positive, asking R to take the square root of a negative value would trigger a warning message and interfere with our operations.
8. Nested for loops
Nested for loops can also be convenient here. Suppose we want to perform an operation for every pair of integers from 1 to 10.
We can accomplish this by writing two for loops, each with an index from 1 to 10.
Although i and j are typically used as indices, they can also be used directly as variables in any desired operations, as shown here.
9. Let's factor some quadratics
Let's code some algebra!