Skip to content

feat: add QuadraticEquationSolver and test cases #5619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@
* [PrimeFactorization](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/PrimeFactorization.java)
* [PronicNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/PronicNumber.java)
* [PythagoreanTriple](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/PythagoreanTriple.java)
* [QuadraticEquationSolver](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/QuadraticEquationSolver.java)
* [ReverseNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/ReverseNumber.java)
* [RomanNumeralUtil](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/RomanNumeralUtil.java)
* [SecondMinMax](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/SecondMinMax.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.thealgorithms.maths;

/**
* This class represents a complex number which has real and imaginary part
*/
class ComplexNumber {
Double real;
Double imaginary;

ComplexNumber(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}

ComplexNumber(double real) {
this.real = real;
this.imaginary = null;
}
}

/**
* Quadratic Equation Formula is used to find
* the roots of a quadratic equation of the form ax^2 + bx + c = 0
*
* @see <a href="https://en.wikipedia.org/wiki/Quadratic_equation">Quadratic Equation</a>
*/
public class QuadraticEquationSolver {
/**
* Function takes in the coefficients of the quadratic equation
*
* @param a is the coefficient of x^2
* @param b is the coefficient of x
* @param c is the constant
* @return roots of the equation which are ComplexNumber type
*/
public ComplexNumber[] solveEquation(double a, double b, double c) {
double discriminant = b * b - 4 * a * c;

// if discriminant is positive, roots will be different
if (discriminant > 0) {
return new ComplexNumber[] {new ComplexNumber((-b + Math.sqrt(discriminant)) / (2 * a)), new ComplexNumber((-b - Math.sqrt(discriminant)) / (2 * a))};
}

// if discriminant is zero, roots will be same
if (discriminant == 0) {
return new ComplexNumber[] {new ComplexNumber((-b) / (2 * a))};
}

// if discriminant is negative, roots will have imaginary parts
if (discriminant < 0) {
double realPart = -b / (2 * a);
double imaginaryPart = Math.sqrt(-discriminant) / (2 * a);

return new ComplexNumber[] {new ComplexNumber(realPart, imaginaryPart), new ComplexNumber(realPart, -imaginaryPart)};
}

// return no roots
return new ComplexNumber[] {};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.thealgorithms.maths;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class QuadraticEquationSolverTest {
private final QuadraticEquationSolver quadraticEquationSolver = new QuadraticEquationSolver();

@Test
public void testSolveEquationRealRoots() {
// 4.2x^2 + 8x + 1.9 = 0
double a = 4.2;
double b = 8;
double c = 1.9;

ComplexNumber[] roots = quadraticEquationSolver.solveEquation(a, b, c);
Assertions.assertEquals(roots.length, 2);
Assertions.assertEquals(roots[0].real, -0.27810465435684306);
Assertions.assertNull(roots[0].imaginary);
Assertions.assertEquals(roots[1].real, -1.6266572504050616);
Assertions.assertNull(roots[1].imaginary);
}

@Test
public void testSolveEquationEqualRoots() {
// x^2 + 2x + 1 = 0
double a = 1;
double b = 2;
double c = 1;

ComplexNumber[] roots = quadraticEquationSolver.solveEquation(a, b, c);
Assertions.assertEquals(roots.length, 1);
Assertions.assertEquals(roots[0].real, -1);
}

@Test
public void testSolveEquationComplexRoots() {
// 2.3x^2 + 4x + 5.6 = 0
double a = 2.3;
double b = 4;
double c = 5.6;

ComplexNumber[] roots = quadraticEquationSolver.solveEquation(a, b, c);
Assertions.assertEquals(roots.length, 2);
Assertions.assertEquals(roots[0].real, -0.8695652173913044);
Assertions.assertEquals(roots[0].imaginary, 1.2956229935435948);
Assertions.assertEquals(roots[1].real, -0.8695652173913044);
Assertions.assertEquals(roots[1].imaginary, -1.2956229935435948);
}
}