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 - 调用 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的变量,并且声明一个结果变量来存储最终结果。
步骤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的信息,您可以浏览这些 教程 。