如何在 Golang 中計算自然數之和?
在本教程中,我們將瞭解如何在 Golang 中找到自然數之和。為此,我們有兩種方法,一種是使用公式本身,另一種是使用 for 迴圈,我們將在本教程中探討這兩種方法。
公式
Sum = ( N * ( N + 1))/2 N = The value of the natural number till which you want to find the sum.
解釋
讓我們使用上述公式找到前 6 個自然數之和。
Sum of first 6 natural numbers = 1 + 2 + 3 + 4 + 5 + 6 = ( N * ( N + 1)) / 2 = ( 6 * ( 6 + 1 )) / 2 = ( 6 * 7) / 2 = 42 / 2 = 21
使用公式查詢自然數之和
演算法
步驟 1 - 宣告儲存我們要查詢其和的數字的變數 N,以及儲存最終結果的 answer 變數。
步驟 2 - 初始化變數 N。
步驟 3 - 呼叫 sumOfNNaturalNumbers() 函式,該函式使用上面提到的公式查詢和。
步驟 4 - 列印結果。
Time Complexity: O(1) Space Complexity: O(1)
示例 1
在本例中,我們將使用上述公式查詢 N 個自然數之和。
package main import "fmt" // fmt package provides the function to print anything // defining the function with a parameter of int32 // type and have a return type int32 func sumOfNNaturalNumbers(N int32) int32 { // declaring the variable sum of int32 type // that will store the sum of N Natural numbers var sum int32 // finding the sum using the formula and storing // into the variable sum = (N * (N + 1)) / 2 // returning the sum return sum } func main() { // declaring the variable N of int32 type till which we // have to find the sum of Natural numbers and a variable // answer that will store the sum var N, answer int32 // initializing the variable N N = 10 fmt.Println("Program to find the sum of the Natural number using the formula.") // calling the sumOfNNaturalNumbers() function and storing // the result in the answer variable answer = sumOfNNaturalNumbers(N) fmt.Println("The sum of", N, "natural numbers is", answer) }
輸出
Program to find the sum of the Natural number using the formula. The sum of 10 natural numbers is 55
使用 for 迴圈查詢自然數之和
演算法
步驟 1 - 宣告儲存我們要查詢其和的數字的變數 N,以及儲存最終結果的 answer 變數。
步驟 2 - 初始化變數 N。
步驟 3 - 呼叫 sumOfNNaturalNumbers() 函式,該函式使用 for 迴圈查詢和。
步驟 4 - 列印結果。
Time Complexity: O(N) Space Complexity: O(1)
示例 2
在本例中,我們將使用 for 迴圈查詢 N 個自然數之和。
package main import "fmt" // fmt package provides the function to print anything // defining the function with a parameter of int32 // type and have a return type int32 func sumOfNNaturalNumbers(N int32) int32 { // declaring the variable sum of int32 type // that will store the sum of N Natural numbers // and a variable iterator that we will use in for loop var iterator, sum int32 // initializing the sum variable with 0 sum = 0 // running a for loop from 1 till N for iterator = 1; iterator <= N; iterator = iterator + 1 { // adding each natural number in the sum sum = sum + iterator } // returning the sum return sum } func main() { // declaring the variable N of int32 type till which we // have to find the sum of Natural numbers and a variable // answer that will store the sum var N, answer int32 // initializing the variable N N = 10 fmt.Println("Program to find the sum of the Natural number using the for loop.") // calling the sumOfNNaturalNumbers() function and storing // the result in the answer variable answer = sumOfNNaturalNumbers(N) fmt.Println("The sum of", N, "natural numbers is", answer) }
輸出
Program to find the sum of the Natural number using the for loop. The sum of 10 natural numbers is 55
結論
這是在 Golang 中查詢自然數之和的兩種方法。第一種方法在時間複雜度方面要好得多。要了解有關 go 的更多資訊,您可以瀏覽這些 教程。
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP