Golang 如何使用递归找到两个给定数字的最大公约数

Golang 如何使用递归找到两个给定数字的最大公约数

在本教程中,我们将看到如何使用递归在Golang语言中找到两个数字的最大公约数。我们将看到两种递归找到两个数字的最大公约数的方法。第一种方法需要更多时间,我们将两个数字中较小的数字减1,然后检查两个数字是否都能被最小数字整除。第二种方法需要更少时间,我们将较大的数字减去较小的数字,直到两个数字相等。

步骤

  • 第1步 - 声明变量用来存储两个数字和答案。

  • 第2步 - 初始化变量。

  • 第3步 - 调用函数来找到最小数字作为每个函数调用的减少数,并且对两个数字进行模运算,如果模为零则返回。

  • 第4步 - 打印结果。

方法1:使用递归函数的非高效方法。

在本例中,我们将两个数字中较小的数字减1,然后检查两个数字是否能被最小数字整除。

示例

package main

// fmt package provides the function to print anything
import (
   "fmt"
)

// this function finds the GCD of two numbers with three parameters
// of int type and have a return type of int type
func gcdOfTwoNumbers(number1, number2, minNumber int) int {

   // checking if the number minNumber can be divided by both number1, and number2
   if minNumber == 1 || (number1%minNumber == 0 && number2%minNumber == 0) {
      return minNumber
   }

   // returning the GCD
   return gcdOfTwoNumbers(number1, number2, minNumber-1)
}
func main() {

   // declaring the variable to store the value of two numbers
   // and a variable to store an answer
   var number1, number2, answer, minNumber int

   // initializing both the variables
   number1 = 20
   number2 = 15
   fmt.Println("Program to find the GCD of two numbers using the recursion function.")
   if number1 < number2 {
      minNumber = number1
   } else {
      minNumber = number2
   }

   // calling a function to find the GCD of two number
   // and passing a minimum of number1 and number2
   answer = gcdOfTwoNumbers(number1, number2, minNumber)

   // printing the result
   fmt.Println("The GCD of", number1, "and", number2, "is", answer)
}

输出

Program to find the GCD of two numbers using the recursion function.
The GCD of 20 and 15 is 5

方法2:使用递归函数的高效方法

在这个示例中,我们将通过从较大的数减去较小的数,直到两个数相等,从而节省时间。

示例

package main

// fmt package provides the function to print anything
import (
   "fmt"
)

// this function finds the GCD of two numbers with two parameters
// of int type and have a return type of int type
func gcdOfTwoNumbers(number1, number2 int) int {

   // returning if both the numbers become equal
   if number1 == number2 {
      return number1
   }

   // reducing the lesser one with the greater one
   if number1 > number2 {
      number1 -= number2
   } else {
      number2 -= number1
   }

   // calling the function
   return gcdOfTwoNumbers(number1, number2)
}
func main() {

   // declaring the variable to store the value of two numbers
   // and a variable to store an answer
   var number1, number2, answer int

   // initializing both the variables
   number1 = 20
   number2 = 15
   fmt.Println("Program to find the GCD of two numbers in efficient way using the recursion function.")

   // calling a function to find the GCD of two number
   answer = gcdOfTwoNumbers(number1, number2)

   // printing the result
   fmt.Println("The GCD of", number1, "and", number2, "is", answer)
}

输出

Program to find the GCD of two numbers in an efficient way using the recursion function.
The GCD of 20 and 15 is 5

结论

这些是使用递归找到两个数的最大公约数的不同方法。第二种方法比第一种更高效。要了解更多关于go的内容,可以探索这些教程。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程