如何在 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

結論

這就是關於查詢兩個數字之間的阿姆斯特朗數的 Golang 程式碼。要了解有關 go 的更多資訊,您可以瀏覽這些教程。

這就是關於查詢兩個數字之間的阿姆斯特朗數的 Golang 程式碼。要了解有關 go 的更多資訊,您可以瀏覽這些 教程

更新於: 2022-08-29

227 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.