如何在 Go 語言中求解一元二次方程的所有根?


在本教程中,我們將使用 Go 程式語言找到一元二次方程的根。本教程包含兩種不同的程式設計方法來找出二次方程的根。

解釋

指數為 2 的方程稱為一元二次方程。一元二次方程的標準方程為 -

$$\mathrm{a\:x\:^{\wedge}\:2\:+\:b\:x\:+\:c\:=\:0}$$

在上述方程中,a、b 和 c 是係數,其中 a 不能為零。為了找到一元二次方程根的性質,即它們是實數還是虛數,我們首先需要使用以下公式找到方程的判別式

$$\mathrm{D\:=\:b\:^{*}\:b\:-\:4\:^{*}\:a\:^{*}\:c}$$

如果 D 大於零,則根是不同的實數。

如果 D 等於零,則根是相等的實數。

如果 D 小於零,則根是虛數。

現在我們將使用以下公式找到方程的根 -

$$\mathrm{X1\:=\:(-\:b\:+\:underRoot\:(b\:^{*}\:b\:-\:4\:^{*}\:a\:^{*}\:c)\:/\:2\:^{*}\:a}$$

$$\mathrm{X1\:=\:(-\:b\:-\:underRoot\:(b\:^{*}\:b\:-\:4\:^{*}\:a\:^{*}\:c)\:/\:2\:^{*}\:a}$$

演算法

步驟 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

結論

這兩種方法都可以在 Go 語言中找到一元二次方程的根。從模組化和程式碼可重用性的角度來看,第二種方法更好,因為我們可以在專案的任何地方呼叫該函式。要了解有關 Go 語言的更多資訊,您可以瀏覽這些 教程。

更新於: 2022-11-29

898 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.