Dadas las raíces de una ecuación cuadrática A y B , la tarea es encontrar la ecuación.
Nota : Las raíces dadas son integrales.
Ejemplos:
Entrada: A = 2, B = 3
Salida: x^2 – (5x) + (6) = 0
x 2 – 5x + 6 = 0
x 2 -3x -2x + 6 = 0
x(x – 3) – 2 (x-3) = 0
(x-3) (x-2) = 0
x = 2, 3Entrada: A = 5, B = 10
Salida: x^2 – (15x) + (50) = 0
Planteamiento: Si las raíces de una ecuación cuadrática ax 2 + bx + c = 0 son A y B entonces se sabe que
A + B = – b/a y A * B = c * a .
Ahora, ax 2 + bx + c = 0 se puede escribir como
x 2 + (b / a)x + (c / a) = 0 (Ya que, a != 0)
x 2 – (A + B)x + ( A * B) = 0, [Ya que, A + B = -b * a y A * B = c * a]
es decir , x 2 – (Suma de las raíces)x + Producto de las raíces = 0
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to find the quadratic // equation whose roots are a and b void findEquation(int a, int b) { int sum = (a + b); int product = (a * b); cout << "x^2 - (" << sum << "x) + (" << product << ") = 0"; } // Driver code int main() { int a = 2, b = 3; findEquation(a, b); return 0; }
Java
// Java implementation of the above approach class GFG { // Function to find the quadratic // equation whose roots are a and b static void findEquation(int a, int b) { int sum = (a + b); int product = (a * b); System.out.println("x^2 - (" + sum + "x) + (" + product + ") = 0"); } // Driver code public static void main(String args[]) { int a = 2, b = 3; findEquation(a, b); } } // This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach # Function to find the quadratic # equation whose roots are a and b def findEquation(a, b): summ = (a + b) product = (a * b) print("x^2 - (", summ, "x) + (", product, ") = 0") # Driver code a = 2 b = 3 findEquation(a, b) # This code is contributed by Mohit Kumar
C#
// C# implementation of the above approach using System; class GFG { // Function to find the quadratic // equation whose roots are a and b static void findEquation(int a, int b) { int sum = (a + b); int product = (a * b); Console.WriteLine("x^2 - (" + sum + "x) + (" + product + ") = 0"); } // Driver code public static void Main() { int a = 2, b = 3; findEquation(a, b); } } // This code is contributed by CodeMech.
Javascript
<script> // Javascript implementation of the above approach // Function to find the quadratic // equation whose roots are a and b function findEquation(a, b) { var sum = (a + b); var product = (a * b); document.write("x^2 - (" + sum + "x) + (" + product + ") = 0"); } // Driver Code var a = 2, b = 3; findEquation(a, b); // This code is contributed by Ankita saini </script>
x^2 - (5x) + (6) = 0
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)