如何在 Golang 中检查两个整数之间的阿姆斯特朗数?


在本教程中,我们将编写并解释查找两个整数之间的阿姆斯特朗数的代码。阿姆斯特朗数是指其所有数字的立方和等于该数字本身的数字。

例如,153 就是一个这样的数字,如下所示

153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

算法

  • 步骤 1 − 首先,我们声明要查找阿姆斯特朗数的两个数字的范围。

  • 步骤 2 − 现在,我们从用户那里获取输入,即要查找阿姆斯特朗数的两个数字。

  • 步骤 3 − 从第一个数字运行到最后一个数字的 for 循环,并调用函数来检查当前数字是否为阿姆斯特朗数,如果是,则打印该数字。

示例

时间复杂度

O(1) - 时间复杂度是常数,因为无论输入是什么,程序都将花费相同的时间。

空间复杂度

O(1) - 程序中的变量是静态的,因此空间复杂度也是常数。

代码

package main // fmt package provides the function to print anything import "fmt" func isArmstrongNumber(num int32) bool { // declaring the sum variable which will store // the sum of the cube of each digit in the number var sum int32 = 0 // declaring and initializing the tempNum variable on which we will // perform some arithmetic operations ahead var tempNum int32 = num // running a for loop till the tempNum become zero for tempNum != 0 { // picking each digit by doing mode on the current number currDigit := tempNum % 10 // adding the cube of the current digit into the number sum = sum + (currDigit * currDigit * currDigit) // eliminating the last digit from the end tempNum = tempNum / 10 } // if the sum is equal to the number then returning true if sum == num { return true } return false } func main() { // declaring the integer number using the var keyword between which we // have to find the Armstrong numbers var number1, number2 int32 fmt.Println("Enter the numbers between which you want to find the Armstrong numbers.") // Taking the input of the integers from the user between which we // have to find the Armstrong numbers fmt.Println("Enter the first number:") fmt.Scanln(&number1) fmt.Println("Enter the second number:") fmt.Scanln(&number2) fmt.Println("The Armstrong number between", number1, "and", number2, "are as follow:") // In this for loop where we are passing each number between the two numbers we have // took from the user for num := number1; num <= number2; num++ { // here we are calling the function to check that the current number is Armstrong // number or not if isArmstrongNumber(num) { fmt.Println(num) } } }

输出

Enter the numbers between which you want to find the Armstrong numbers.
Enter the first number:
0
Enter the second number:
10000
The Armstrong number between 0 and 10000 are as follow:
0
1
153
370
371
407

代码描述

  • var number1, number2 int32 - 这行代码声明了两个 int32 类型的变量。我们要在这两个数字之间找到所有阿姆斯特朗数。

  • fmt.Scanln(<number1) 和 fmt.Scanln(<number2)- 在这里,我们从用户那里获取输入。

  • for num := number1; num <= number2; num++ {} - 这个 for 循环从 number1 运行到 number2。

  • if isArmstrongNumber(num) {} - 在这个 if 条件中,调用了 isArmstrongNumber() 函数,并传递了参数 num,它是 for 循环的当前索引。if 条件正在检查 isArmstrongNumber() 函数返回的值。如果值为真,则我们打印该数字。

  • func isArmstrongNumber(num int32) bool {} - 这是 isArmstrongNumber() 函数。它包含一个数据类型为 int32 的 num 参数,并具有布尔类型的返回值。

    • var sum int32 = 0 - 在这里,我们声明了 int32 类型的 sum 变量,它将存储数字中每个数字的立方和。

    • var tempNum int32 = num - 声明 int32 类型的 tempNum 变量,并用 num 值初始化。我们将对 tempNum 执行算术运算,这就是为什么我们不直接对 num 变量执行这些算术运算的原因,num 变量稍后将与 sum 进行比较。

    • for tempNum != 0 {} - 这个 for 循环一直运行到 tempNum 变为零。

    • currDigit := tempNum % 10 - 我们通过对 10 应用 % 并将其存储在 currDigit 中来获取当前数字的最后一位数字。

    • sum = sum + (currDigit * currDigit * currDigit) - 将 currDigit 的立方加到 sum 变量中。

    • tempNum = tempNum / 10 - 将 tempNum 除以 10,以便从值中删除最后一位数字。

    • if sum == num {} - 最后,我们将 sum 与数字进行比较,并返回真或假。

逻辑解释

数字是阿姆斯特朗数

假设我们有一个数字 371,我们需要检查它是否是阿姆斯特朗数。

  • 通过执行 % 获取最后一位数字 - 371 % 10 = 1

    Sum = 0 + (1*1*1) -> sum = 1

    Num -> 371 /10 = 37

  • 通过执行 % 获取最后一位数字 - 37 % 10 = 7

    Sum = 1 + (7*7*7) -> sum = 344

    Num -> 37 /10 = 3

  • 通过执行 % 获取最后一位数字 - 3 % 10 = 3

    Sum = 344 + (3*3*3) -> sum = 371

    Num -> 3 /10 = 0

如您所见,sum 等于初始数字,这就是 371 是阿姆斯特朗数的原因。

数字不是阿姆斯特朗数

假设我们有一个数字 251,我们需要检查它是否是阿姆斯特朗数。

  • 通过执行 % 获取最后一位数字 - 251 % 10 = 1

    Sum = 0 + (1*1*1) -> sum = 1

    Num -> 251 /10 = 25

  • 通过执行 % 获取最后一位数字 - 25 % 10 = 5

    Sum = 1 + (5*5*5) -> sum = 126

    Num -> 25 /10 = 2

  • 通过执行 % 获取最后一位数字 - 2 % 10 = 2

    Sum = 126 + (2*2*2) -> sum = 134

    Num -> 2 /10 = 0

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

结论

这就是关于查找两个数字之间的阿姆斯特朗数的 Golang 代码。要了解有关 go 的更多信息,您可以浏览这些教程。

这就是关于查找两个数字之间的阿姆斯特朗数的 Golang 代码。要了解有关 go 的更多信息,您可以浏览这些 教程

更新于: 2022-08-29

227 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告