Skip to content

Instantly share code, notes, and snippets.

@sedera-tax
Created January 15, 2021 13:41
Show Gist options
  • Save sedera-tax/57a2f19f4f18d43d6cdee0c05be029ac to your computer and use it in GitHub Desktop.
Save sedera-tax/57a2f19f4f18d43d6cdee0c05be029ac to your computer and use it in GitHub Desktop.
3. Quadratic Equation Implement the function findRoots to find the roots of the quadratic equation: ax2 + bx + c = 0. The function should return an array containing both roots in any order. If the equation has only one solution, the function should return that solution as both elements of the array. The equation will always have at least one sol…
<?php
/**
* @return array An array of two elements containing roots in any order
*/
function findRoots($a, $b, $c)
{
$delta = ($b * $b) - 4 * ($a * $c);
$x = (- $b - sqrt($delta)) / (2 * $a);
$y = (- $b + sqrt($delta)) / (2 * $a);
return [$x, $y];
}
print_r(findRoots(2, 10, 8));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment