Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Probability Puzzles in R

Cursus bekijken

Oefeninstructies

  • 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.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

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(___)    
  }
}
Code bewerken en uitvoeren