Golang 如何检查输入的数字是否为霓虹灯数字
在本教程中,我们将编写并解释检查给定数字是否为霓虹灯数字的代码。霓虹灯数字是一个等于其所有数字的平方之和的数字。
例如,9是一个霓虹灯数字,如下所示
9的平方是。
9 * 9 = 81
正方形的每个数字之和即81是9,这个数字也是9,所以9是一个霓虹灯数字。
算法
- 第1步 – 首先,我们要声明我们必须在这些数字之间找到霓虹灯号码。
-
第2步 – 现在,我们接受用户的输入,我们必须检查它是否是一个霓虹灯数字。
-
第3步 – 调用isNeonNumber(num int32)函数,检查当前的数字是否是霓虹灯数字,如果是,则打印该数字。
例子
在这个例子中,我们要找出两个整数之间的霓虹灯数字,这些数字是由用户输入的。
时间复杂度
O(1) – 时间复杂度是恒定的,因为无论输入什么,程序都需要正常的时间。
空间复杂度
O(1) – 程序中的变量是静态的,所以空间复杂性也是恒定的。
package main
// fmt package provides the function to print anything
import "fmt"
func isNeonNumber(num int32) bool {
// declaring the sum variable which will store
// the sum of each digit in the square of the number
var sum int32 = 0
// declaring and initializing the tempNum variable with the square of num
// on which we will perform some arithmetic operations ahead
var tempNum int32 = num * 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 current digit into the sum variable
sum = sum + 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 Neon numbers
var number int32
fmt.Println("Program to check whether the given number is a Neon number or not.")
fmt.Println("Enter the number to check whether it is the Neon number or not.")
// Taking the input from the user
fmt.Scanln(&number)
if isNeonNumber(number) {
fmt.Println(number, "is a Neon number.")
} else {
fmt.Println(number, "is not a Neon number.")
}
}
输出1
Program to check whether the given number is a Neon number or not.
Enter the number to check whether it is the Neon number or not.
9
9 is a Neon number.
输出2
Program to check whether the given number is a Neon number or not.
Enter the number to check whether it is the Neon number or not.
202
202 is not a Neon number.
代码的描述
- var number int32 – 这行代码是在声明int32变量。我们将检查这个数字是否是一个霓虹灯数字。
-
fmt.Scanln(&number)- 在这里我们从用户那里获得输入。
-
if isNeonNumber(number) {} – 在这个if条件中,isNeonNumber()函数被调用并传递参数num,它是for循环的当前索引。这个if条件是检查isNeonNumber()函数的返回值。如果该值为真,那么我们将打印该数字。
-
func isNeonNumber(num int32) bool {} – 这是 isNeonNumber() 函数。它由一个数据类型为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加到sum变量中。
-
tempNum = tempNum / 10 – 将tempNum除以10,这样最后一位数字就会从数值中删除。
-
if sum == num {} – 最后,我们将sum与数字进行比较,并返回true或false。
逻辑解释
数字是霓虹灯
假设我们有一个数字371,我们必须检查这个数字是否是Armstrong。
- 通过做%-81%10=1来获取最后一个数字 总和=0+1->总和=1
数值 -> 81 /10 = 8
- 通过做% – 8 % 10 = 8来获取最后一个数字 总和 = 1 + 8 -> 总和 = 9
数值 -> 8 /10 = 0
正如你所看到的,总和等于最初的数字,这就是为什么9是一个霓虹灯数字。
数字不是霓虹灯
假设我们有一个数字12,我们必须检查这个数字是否是霓虹灯。
12的平方是144,现在我们要找出144的每个数字之和。
- 通过做%-144%10=4来获取最后一个数字 总和=0+4->总和=4
数值 -> 144 /10 = 14
- 通过做 % -14 % 10 = 4 来获取最后一个数字 总数 = 4 + 4 -> 总数 = 8
数值 -> 14 /10 = 1
- 通过做% – 1 % 10 = 1来获取最后一个数字 和 = 8 + 1 -> 和 = 9
数值 -> 1 /10 = 0
结论
正如你所看到的,总和不等于最初的数字,这就是为什么12不是一个Neon数字。这就是关于Golang代码的全部内容,用来检查给定的数字是否是氖型数字。