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

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

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

例如,153是一个数字,如下图所示

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

算法

  • 第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)
         }
      }
   }
Go

输出

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
Go

代码的描述

  • 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循环的当前索引。如果条件是检查isArmstrongNumber()函数的返回值。如果该值为真,我们将打印该数字。

  • func isArmstrongNumber(num int32) bool {} – 这就是isArmstrongNumber()函数。它由一个数据类型为int32的num参数组成,其返回类型为bool。

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

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

    • for tempNum != 0 {} – 这个for循环一直运行到tempNum变成0。

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

    • sum = sum + (currDigit * currDigit * currDigit) – 将currDigit的立方体加入sum变量中。

    • tempNum = tempNum / 10 – 将tempNum除以10,这样最后一位数字就会从数值中删除。

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

逻辑解释

数字是阿姆斯特朗

假设我们有一个数字371,我们必须检查这个数字是否是阿姆斯特朗。

  • 通过做% – 371 % 10 = 1来获取最后一个数字。

和 = 0 + (111) -> 和 = 1

数值 -> 371 /10 = 37

  • 通过做%-37%10=7来取最后一个数字

和 = 1 + (777) -> 和 = 344

数值 -> 37 /10 = 3

  • 通过做% – 3 % 10 = 3来取最后一个数字

和 = 344 + (333) -> 和 = 371

数值 -> 3 /10 = 0

正如你所看到的,和等于初始数字,这就是为什么371是一个阿姆斯特朗数字。

数字不是阿姆斯特朗的

假设我们有一个数字251,我们必须检查这个数字是否是阿姆斯特朗。

  • 通过做%-251%10=1来取最后一个数字

和=0+(111)->和=1

Num -> 251 /10 = 25

  • 通过做%-25%10=5来取最后一个数字

和 = 1 + (555) -> 总数 = 126

数值 -> 25 /10 = 2

  • 通过做% – 2 % 10 = 2来取最后一个数字

总和=126 + (222) -> 总和=134

数值 -> 2 /10 = 0

总结

这就是关于Golang代码的全部内容,用来寻找两个数字之间的阿姆斯特朗数字。要了解更多关于Go的知识,你可以探索这些教程。

这是所有关于Golang的代码,用于查找两个数字之间的阿姆斯特朗数字。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册