Write a function to check factorability
Let us write a function to evaluate whether any given quadratic is factorable or not. The arguments to the function will be the coefficient values a
b
and c
for a given quadratic of the form
$$ ax^2 + bx + c
$$
and we will determine whether the discriminant of the quadratic formula $$ b^2 - 4ac $$
is a perfect square. If it is, then the quadratic is factorable.
This exercise is part of the course
Probability Puzzles in R
Exercise instructions
- Fill in the condition to check whether the solutions to the quadratic equation are imaginary.
- Write the conditional statement to specify that the next section should run only when the previous condition is
FALSE
. - Return the result of the check for whether the discriminant is a perfect square, thus making the quadratic factorable.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
is_factorable <- function(a,b,c){
# Check whether solutions are imaginary
if(b^2 - 4*a*c ___){
return(FALSE)
# Designate when the next section should run
} ___ {
sqrt_discriminant <- sqrt(b^2 - 4*a*c)
# return TRUE if quadratic is factorable
return(___)
}
}