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,该变量用于存储我们要找到的数字的总和,同时也用于存储最终结果的答案变量。
第2步 - 初始化变量N。
第3步 – 调用 sumOfNaturalNumbers() 函数,用上面提到的公式求和。
第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,该变量存储了我们要找到的数字的总和,同时也声明了答案变量,以存储最终的结果。
第2步 - 初始化变量N。
第3步 – 调用 sumOfNaturalNumbers() 函数,使用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中寻找自然数之和的两种方法。第一种方法在时间复杂度方面要好得多。