Check if roots of a Quadratic Equation are reciprocal of each other or not
Given three numbers A, B, C which represents the coefficients(constants) of a quadratic equation , the task is to check whether the roots of the equation represented by these constants are reciprocal of each other or not.
Examples:
Input: A = 2, B = -5, C = 2
Output: Yes
Explanation:
The given quadratic equation is .
Its roots are (1, 1/1) which are reciprocal of each other.
Input: A = 1, B = -5, C = 6
Output: No
Explanation:
The given quadratic equation is .
Its roots are (2, 3) which are not reciprocal of each other.
Approach: The idea is to use the concept of quadratic roots to solve the problem. We can formulate the condition required to check whether one root is the reciprocal of the other or not by:
- Let the roots of the equation be and .
- The product of the roots of the above equation is given by * .
- It is known that the product of the roots is C/A. Therefore, the required condition is C = A.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void checkSolution( int a, int b, int c)
{
if (a == c)
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
int a = 2, b = 0, c = 2;
checkSolution(a, b, c);
return 0;
}
|
Java
class GFG{
static void checkSolution( int a, int b, int c)
{
if (a == c)
System.out.print( "Yes" );
else
System.out.print( "No" );
}
public static void main(String[] args)
{
int a = 2 , b = 0 , c = 2 ;
checkSolution(a, b, c);
}
}
|
Python3
def checkSolution(a, b, c):
if (a = = c):
print ( "Yes" );
else :
print ( "No" );
a = 2 ; b = 0 ; c = 2 ;
checkSolution(a, b, c);
|
C#
using System;
class GFG{
static void checkSolution( int a, int b, int c)
{
if (a == c)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
public static void Main()
{
int a = 2, b = 0, c = 2;
checkSolution(a, b, c);
}
}
|
Javascript
<script>
function checkSolution(a, b, c)
{
if (a == c)
document.write( "Yes" );
else
document.write( "No" );
}
let a = 2, b = 0, c = 2;
checkSolution(a, b, c);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Using Quadratic Formula in python:
We can find the roots of a quadratic equation using the quadratic formula:
x = (-b ± sqrt(b^2 – 4ac)) / 2a
If the roots are reciprocal of each other, then one of the roots is the reciprocal of the other:
x1 * x2 = 1
x2 = 1 / x1
Substituting x2 in terms of x1 in the above quadratic formula:
x1 = (-b ± sqrt(b^2 – 4ac)) / 2a
1 / x1 = (-b ? sqrt(b^2 – 4ac)) / 2a
So, we can find the roots using the quadratic formula and check if they satisfy the above condition to be reciprocal of each other.
- Calculate the discriminant of the quadratic equation using the formula discriminant = B**2 – 4*A*C.
- If the discriminant is negative, return False as the quadratic equation has no real roots.
- Calculate the roots of the quadratic equation using the formula root1 = (-B + math.sqrt(discriminant)) / (2*A) and root2 = (-B – math.sqrt(discriminant)) / (2*A).
- Check if the product of the roots root1 * root2 is equal to 1.
- If the product is equal to 1, return True as the roots are reciprocal of each other. Otherwise, return False.
C++
#include <iostream>
#include <cmath>
bool areRootsReciprocal( double A, double B, double C) {
double discriminant = B * B - 4 * A * C;
if (discriminant < 0) {
return false ;
}
double root1 = (-B + sqrt (discriminant)) / (2 * A);
double root2 = (-B - sqrt (discriminant)) / (2 * A);
return root1 * root2 == 1;
}
int main() {
double A1 = 2, B1 = -5, C1 = 2;
double A2 = 1, B2 = -5, C2 = 6;
if (areRootsReciprocal(A1, B1, C1)) {
std::cout << "Yes" << std::endl;
} else {
std::cout << "No" << std::endl;
}
if (areRootsReciprocal(A2, B2, C2)) {
std::cout << "Yes" << std::endl;
} else {
std::cout << "No" << std::endl;
}
return 0;
}
|
Java
public class Main {
public static boolean areRootsReciprocal( double A, double B, double C) {
double discriminant = B * B - 4 * A * C;
if (discriminant < 0 ) {
return false ;
}
double root1 = (-B + Math.sqrt(discriminant)) / ( 2 * A);
double root2 = (-B - Math.sqrt(discriminant)) / ( 2 * A);
return root1 * root2 == 1 ;
}
public static void main(String[] args) {
double A1 = 2 , B1 = - 5 , C1 = 2 ;
double A2 = 1 , B2 = - 5 , C2 = 6 ;
if (areRootsReciprocal(A1, B1, C1)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
if (areRootsReciprocal(A2, B2, C2)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
Python3
import math
def are_roots_reciprocal(A, B, C):
discriminant = B * * 2 - 4 * A * C
if discriminant < 0 :
return False
root1 = ( - B + math.sqrt(discriminant)) / ( 2 * A)
root2 = ( - B - math.sqrt(discriminant)) / ( 2 * A)
return root1 * root2 = = 1
A1, B1, C1 = 2 , - 5 , 2
A2, B2, C2 = 1 , - 5 , 6
if are_roots_reciprocal(A1, B1, C1):
print ( "Yes" )
else :
print ( "No" )
if are_roots_reciprocal(A2, B2, C2):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class MainClass {
static bool AreRootsReciprocal( double A, double B,
double C)
{
double discriminant = B * B - 4 * A * C;
if (discriminant < 0) {
return false ;
}
double root1
= (-B + Math.Sqrt(discriminant)) / (2 * A);
double root2
= (-B - Math.Sqrt(discriminant)) / (2 * A);
return root1 * root2 == 1;
}
public static void Main( string [] args)
{
double A1 = 2, B1 = -5, C1 = 2;
double A2 = 1, B2 = -5, C2 = 6;
if (AreRootsReciprocal(A1, B1, C1)) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
if (AreRootsReciprocal(A2, B2, C2)) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
}
}
|
Javascript
function areRootsReciprocal(A, B, C) {
let discriminant = B ** 2 - 4 * A * C;
if (discriminant < 0) {
return false ;
}
let root1 = (-B + Math.sqrt(discriminant)) / (2 * A);
let root2 = (-B - Math.sqrt(discriminant)) / (2 * A);
return root1 * root2 === 1;
}
let A1 = 2, B1 = -5, C1 = 2;
let A2 = 1, B2 = -5, C2 = 6;
if (areRootsReciprocal(A1, B1, C1)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
if (areRootsReciprocal(A2, B2, C2)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Time Complexity: O(1)
Space Complexity: O(1)
Last Updated :
13 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...