如何在 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 的更多信息,您可以浏览这些 教程。
广告