Golang 如何查找一元二次方程的所有根
在本教程中,我们将使用Golang编程语言查找一元二次方程的根。本教程包括两种不同的编程方式来找出一元二次方程的根。
解释
幂为2的方程称为一元二次方程。二次方程的标准方程是-

在上述方程中,a、b和c是系数,其中a不能为零。要找到二次方程的根的性质,即它们是实数还是虚数,首先我们需要用以下公式找到方程的判别式

如果D大于零,那么根是不同的,并且是实数。
如果D等于零,那么根是相等的并且是实数。
如果D小于零,那么根是虚数。
现在我们将用下面的公式找出方程的根:


算法
第1步 --声明系数和判别式的变量。
第2步 --用各自的值初始化变量。
第3步 - 用各自的公式找到判别式。
第4步 - 根据判别式的值来寻找根。
例子
在这个例子中,我们将在函数中找到二次方程的根。
package main
import (
// fmt package provides the function to print anything
"fmt"
// math package is to use all the math-related predefined functions
"math"
)
func main() {
// declaring the variables to store the value
// of the coefficients of quadratic equation
var a, b, c float64
// declaring a variable of float type to store discriminant
// of the quadratic equation
var d float64
// initializing the coefficients variable with respective value
a = 1
b = 1
c = 1
fmt.Println("Finding the roots of the equation with the below coefficients within the function:")
fmt.Println("a =", a)
fmt.Println("b =", b)
fmt.Println("c =", c)
// finding the discriminant using the respective formulae
d = b*b - 4*a*c
if d > 0 {
fmt.Println("Roots are real and different.")
root1 := (-b + math.Sqrt(d)) / (2 * a)
root2 := (-b - math.Sqrt(d)) / (2 * a)
fmt.Println("The roots are:")
fmt.Println("First Root", root1)
fmt.Println("Second Root", root2)
} else if d == 0 {
fmt.Println("Roots are equal and same.")
root1 := (-b + math.Sqrt(d)) / (2 * a)
fmt.Println("The root is", root1)
} else {
fmt.Println("Roots are complex.")
realPart := -b / (2 * a)
imaginaryPart := math.Sqrt(math.Abs(d)) / (2 * a)
fmt.Println("The roots are:")
fmt.Println("First Root", realPart, "+", "i", imaginaryPart)
fmt.Println("Second Root", realPart, "-", "i", imaginaryPart)
}
}
输出
Finding the roots of the equation with the below coefficients within the function:
a = 1
b = 1
c = 1
Roots are complex.
The roots are:
First Root -0.5 + i 0.8660254037844386
Second Root -0.5 - i 0.8660254037844386
算法
第1步 - 声明系数和判别符的变量。
第2步 --用各自的值初始化变量。
第3步 - 用各自的公式找到判别式。
第4步 - 根据判别式的值来寻找根。
例子
在这个例子中,我们将在单独的函数中找到二次方程的根。
package main
import (
// fmt package provides the function to print anything
"fmt"
// math package is to use all the math related predefined functions
"math"
)
func rootsOfQuadraticEquation(a, b, c float64) {
// declaring a variable of float type to store discriminant
// of the quadratic equation
var d float64
// finding the discriminant using the respective formulae
d = b*b - 4*a*c
if d > 0 {
fmt.Println("Roots are real and different.")
root1 := (-b + math.Sqrt(d)) / (2 * a)
root2 := (-b - math.Sqrt(d)) / (2 * a)
fmt.Println("The roots are:")
fmt.Println("First Root", root1)
fmt.Println("Second Root", root2)
} else if d == 0 {
fmt.Println("Roots are equal and same.")
root1 := (-b + math.Sqrt(d)) / (2 * a)
fmt.Println("The root is", root1)
} else {
fmt.Println("Roots are complex.")
realPart := -b / (2 * a)
imaginaryPart := math.Sqrt(math.Abs(d)) / (2 * a)
fmt.Println("The roots are:")
fmt.Println("First Root", realPart, "+", "i", imaginaryPart)
fmt.Println("Second Root", realPart, "-", "i", imaginaryPart)
}
}
func main() {
// declaring the variables to store the value
// of the coefficients of quadratic equation
var a, b, c float64
// initializing the coefficients variable with respective value
a = 4
b = 12
c = 9
fmt.Println("Finding the roots of the equation with the below coefficients in the seperate function:")
fmt.Println("a =", a)
fmt.Println("b =", b)
fmt.Println("c =", c)
rootsOfQuadraticEquation(a, b, c)
}
输出
Finding the roots of the equation with the below coefficients in the seperate function:
a = 4
b = 12
c = 9Roots are equal and same.
The root is -1.5
结论
以上是在Golang中寻找二次方程根的两种方法。第二种方法在模块化和代码重用性方面要好得多,因为我们可以在项目的任何地方调用这个函数。
极客教程