如何在Go語言中檢查輸入數字是否為Neon數?


在本教程中,我們將編寫並解釋程式碼,以檢查給定數字是否為Neon數。Neon數是一個等於其平方所有數字之和的數。

例如,9是一個Neon數,如下所示

The square of 9 is: 9 * 9 = 81 The sum of each digit of the square i.e 81 is 9 and the number is also 9 so 9 is a Neon number.

演算法

  • 步驟1 - 首先,我們宣告要查詢Neon數的範圍。

  • 步驟2 - 現在,我們從使用者那裡獲取輸入,檢查它是否為Neon數。

  • 步驟3 - 呼叫isNeonNumber(num int32)函式,並檢查當前數字是否為Neon數,如果是,則列印該數字。

示例

在這個例子中,我們將找到使用者輸入的兩個整數之間的Neon數。

時間複雜度

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型別的變數。我們將檢查這個數字是否為Neon數。

  • fmt.Scanln(&number)- 在這裡,我們從使用者那裡獲取輸入。

  • if isNeonNumber(number) {} - 在這個if條件中,呼叫isNeonNumber()函式並傳遞引數num(這是for迴圈的當前索引)。if條件檢查isNeonNumber()函式返回的值。如果值為true,則列印該數字。

  • func isNeonNumber(num int32) bool {} - 這是isNeonNumber()函式。它包含一個int32型別的num引數,返回值型別為bool。

    • var sum int32 = 0 - 在這裡,我們宣告一個int32型別的sum變數,它將儲存數字平方的數字之和。

    • var tempNum int32 = num - 宣告一個int32型別的tempNum變數,它被num的平方值初始化。我們將對tempNum進行算術運算,這就是為什麼我們不直接對稍後將與sum比較的num變數進行這些算術運算的原因。

    • for tempNum != 0 {} - 這個for迴圈一直執行到tempNum變為零。

    • currDigit := tempNum % 10 - 我們透過對10應用%運算子並將其儲存在currDigit中來獲取當前數字的最後一位數字。

    • sum = sum + currDigit - 將currDigit新增到sum變數。

    • tempNum = tempNum / 10 - 將tempNum除以10,以便從該值中刪除最後一位數字。

    • if sum == num {} - 最後,我們將sum與數字進行比較,並返回true或false。

邏輯解釋

數字是Neon數

假設我們有一個數字371,我們必須檢查它是否為Armstrong數。

  • 透過執行%運算子獲取最後一位數字 - 81 % 10 = 1 Sum = 0 + 1 -> sum = 1

    Num -> 81 /10 = 8

  • 透過執行%運算子獲取最後一位數字 - 8 % 10 = 8 Sum = 1 + 8 -> sum = 9

    Num -> 8 /10 = 0

如你所見,sum等於初始數字,這就是為什麼9是Neon數。

數字不是Neon數

假設我們有一個數字12,我們必須檢查它是否為Neon數。

12的平方是144,現在我們將找到144的每位數字之和。

  • 透過執行%運算子獲取最後一位數字 -144 % 10 = 4 Sum = 0 + 4 -> sum = 4

    Num -> 144 /10 = 14

  • 透過執行%運算子獲取最後一位數字 - 14 % 10 = 4 Sum = 4 + 4 -> sum = 8

    Num -> 14 /10 = 1

  • 透過執行%運算子獲取最後一位數字 - 1 % 10 = 1 Sum = 8 + 1 -> sum = 9

    Num -> 1 /10 = 0

結論

如你所見,sum不等於初始數字,這就是為什麼12不是Neon數。這就是關於Go語言程式碼檢查給定數字是否為Neon數的全部內容。要了解更多關於Go的資訊,你可以瀏覽這些教程

更新於:2022年8月29日

瀏覽量:258

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.