使用递归的Go语言程序计算幂
在本教程中,我们将学习如何在Go编程语言中使用递归技术计算幂。
幂可以定义为一个数自身相乘特定次数的结果。
指数可以定义为一个数在乘法中使用的次数。幂和指数是改写数学中冗长乘法问题的重要工具,尤其是在代数中。
例如:24 = 2 × 2 × 2 × 2 = 16,其中2是底数,4是指数。
递归是指函数通过直接或间接方式调用自身。每个递归函数都有一个基本情况或基本条件,这是递归中的最终可执行语句,并停止进一步的调用。
下面我们展示了两种不同类型的递归方法的示例。
示例1:使用直接递归方法计算幂的Go语言程序代码
语法
Result = (num * POWER(num, power-1) // Recursive function call to the function POWER() by itself up to the defined condition
算法
步骤1 − 导入包fmt。
步骤2 − 创建函数POWER()。
步骤3 − 我们将使用if条件语句。
步骤4 − 对函数本身进行递归调用。
步骤5 − 启动函数main()。
步骤6 − 声明并初始化变量。
步骤7 − 调用函数POWER()。
步骤8 − 使用fmt.Printf()在屏幕上打印结果。
示例
// GOLANG PROGRAM TO CALCULATE THE POWER USING RECURSION // Direct Recursion example package main // fmt package provides the function to print anything import "fmt" // create a function func POWER(num int, power int) int { var result int = 1 if power != 0 { // Recursive function call to itself result = (num * POWER(num, power-1)) } return result } func main() { fmt.Println("Golang Program to calculate the power using recursion") // declare and initialize the integer variables var base int = 4 var power int = 2 var result int // calling the POWER() function result = POWER(base, power) // Print the result using in-built function fmt.Printf() fmt.Printf("%d to the power of %d is: %d\n", base, power, result) }
输出
Golang Program to calculate the power using recursion 4 to the power of 2 is: 16
代码描述
在上面的程序中,我们首先声明包main。
我们导入了包含fmt包文件的fmt包。
接下来,我们创建一个函数POWER(),使用直接递归技术计算幂。
我们将使用一个if条件语句,如果指定的条件为真,则允许执行一段代码,然后递归调用函数本身。
现在启动函数main()。GO程序执行从函数main()开始。声明整数变量base、power和result。
现在调用POWER()函数。
最后,使用内置函数fmt.Printf()在屏幕上打印结果。此函数在fmt包下定义,它有助于写入标准输出。
示例2:使用间接递归方法计算幂的Go语言程序代码
语法
func recursion_1() {
recursion_2()}
func recursion_2(){
recursion_1()}
func main() {
recursion_1();
}
算法
步骤1 − 导入包fmt。
步骤2 − 创建函数POWER_1()。
步骤3 − 我们将使用if条件语句。
步骤4 − 递归调用函数POWER_2()。
步骤5 − 创建函数POWER_2()。
步骤6 − 间接递归调用函数POWER_1()。
步骤7 − 启动函数main()。
步骤8 − 声明并初始化变量。
步骤9 − 调用函数POWER_2()。
步骤10 − 使用fmt.Printf()在屏幕上打印结果。
示例
// GOLANG PROGRAM TO CALCULATE THE POWER USING RECURSION // Indirect Recursion example package main // fmt package provides the function to print anything import "fmt" // create a first Recursive function func POWER_1(num int, power int) int { var result int = 1 if power != 0 { // Recursive function call to the second function result = (num * POWER_2(num, power-1)) } return result } // create a second Recursive function func POWER_2(num int, power int) int { var result int = 1 if power != 0 { // Recursive function call to the first function // which calls this first function indirectly result = (num * POWER_1(num, power-1)) } return result } func main() { fmt.Println("Golang Program to calculate the power using recursion") // declare and initialize the integer variables var base int = 5 var power int = 2 var result int // calling the POWER_2() function result = POWER_2(base, power) // Print the result using in-built function fmt.Printf() fmt.Printf("%d to the power of %d is: %d\n", base, power, result) }
输出
Golang Program to calculate the power using recursion 5 to the power of 2 is: 25
代码描述
在上面的程序中,我们首先声明包main。
我们导入了包含fmt包文件的fmt包。
接下来,我们创建一个函数POWER_1(),使用间接递归技术计算幂。
我们将使用一个if条件语句,如果指定的条件为真,则允许执行一段代码,然后递归调用第二个函数POWER_2()。
接下来,我们创建一个函数POWER_2()。这里对第一个函数进行了递归函数调用,间接调用了第一个函数POWER_1()。
现在启动函数main()。GO程序执行从函数main()开始。
声明整数变量base、power和result。
现在调用POWER_2()函数。
最后,使用内置函数fmt.Printf()在屏幕上打印结果。此函数在fmt包下定义,它有助于写入标准输出。
结论
在以上两个例子中,我们已经成功编译并执行了Go语言程序代码,使用递归技术计算幂。在第一个例子中,我们展示了直接递归方法,在第二个例子中,我们展示了间接递归方法。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP