2023-06-06 22:56:56 +00:00
|
|
|
namespace LensSimulatorCore.MathsLib;
|
|
|
|
|
|
|
|
public static class CoreMaths
|
|
|
|
{
|
|
|
|
public static double CalculateLCM(double num1, double num2)
|
|
|
|
{
|
|
|
|
// Find the greater and lesser numbers
|
|
|
|
double greaterNum = Math.Max(num1, num2);
|
|
|
|
double lesserNum = Math.Min(num1, num2);
|
|
|
|
|
|
|
|
// Calculate LCM using the formula: LCM = (num1 * num2) / GCD(num1, num2)
|
|
|
|
double lcm = (greaterNum * lesserNum) / CalculateGCD(greaterNum, lesserNum);
|
|
|
|
|
|
|
|
// Return the LCM
|
|
|
|
return lcm;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static double CalculateGCD(double num1, double num2)
|
|
|
|
{
|
|
|
|
// Euclidean algorithm to calculate GCD
|
|
|
|
while (num2 != 0)
|
|
|
|
{
|
|
|
|
double temp = num2;
|
|
|
|
num2 = num1 % num2;
|
|
|
|
num1 = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the GCD
|
|
|
|
return num1;
|
|
|
|
}
|
|
|
|
|
2023-06-07 19:16:35 +00:00
|
|
|
public static List<double> SolveQuadratic(double a, double b, double c)
|
2023-06-06 22:56:56 +00:00
|
|
|
{
|
2023-06-07 19:16:35 +00:00
|
|
|
var solutions = new List<double>();
|
2023-06-06 22:56:56 +00:00
|
|
|
|
|
|
|
double discriminant = b * b - 4 * a * c;
|
|
|
|
if (discriminant < 0)
|
|
|
|
{
|
|
|
|
// If the discriminant is less than zero, then there are no real solutions
|
|
|
|
return solutions;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Calculate both solutions
|
|
|
|
double sqrtDiscriminant = Math.Sqrt(discriminant);
|
|
|
|
double solution1 = (-b + sqrtDiscriminant) / (2 * a);
|
|
|
|
double solution2 = (-b - sqrtDiscriminant) / (2 * a);
|
|
|
|
solutions.Add(solution1);
|
|
|
|
solutions.Add(solution2);
|
|
|
|
return solutions;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|