Golang程序 计算N以内所有奇数之和
在本教程中,我们将讨论用Golang编程寻找N以内所有奇数之和的程序。
但在编写代码之前,让我们先简单讨论一下奇数的问题。
奇数
奇数是一个不能完全被2整除的数字,当这个数字被2整除时,它的余数总是1.检查一个数字是否是奇数的简单方法是检查它的最后一位数字。如果最后一位数字是1、3、5、7或9,那么这个数字就被称为奇数。
方法一:遍历前n个奇数,然后相加
例子
- 数字=4
前4个奇数的总和=16
前4个奇数是:1、3、5、7
这些数字的总和=1+3+5+7
= 16
算法
第1步 - 声明一个变量’N’用于存储数字。
第2步 - 声明一个变量’nextOdd’用于存储数字1。
第3步 - 声明一个变量’sum’用于存储奇数的总和,并将其初始化为0。
第4步 - 创建一个for循环,从1开始运行,直到它小于或等于’N’。
第5步 - 通过将下一个奇数加到总和中来计算总和,直到for循环结束。
第6步 - 所有奇数的差值都是2,因此计算下一个奇数时要在现有数字上加2。
第7步 - 一旦for循环结束,打印存储在’sum’中的结果。
例子
package main
// fmt package allows us to print formatted strings
import "fmt"
func main() {
fmt.Println("Program to calculate the sum of all odd numbers up to N \n")
// declaring variable ‘N’ for storing the number
var N int = 10
// declaring variable ‘nextOdd’ for storing the next odd number
var nextOdd int = 1
// declaring variable ‘sum’ for storing the sum of all odd numbers up to N
var sum int = 0
for x := 1; x <= N; x++ {
// calculating the I’m sum of odd numbers
sum = sum + nextOdd
// calculating the next odd number
nextOdd = nextOdd + 2
}
fmt.Println("Finding the sum of first", N, "Odd numbers")
fmt.Println("Therefore, Sum of all odd numbers up to N : ", sum)
}
输出
Program to calculate the sum of all odd numbers up to N
Finding the sum of first 10 Odd numbers
Therefore, Sum of all odd numbers up to N : 100
代码的描述
- var nextOdd int = 1 – 我们将在这个变量中存储下一个奇数的值。最初,我们分配给它的值是1,因为奇数是从数字1开始的。
-
var sum int = 0 – 我们将在这个变量中存储奇数的结果之和,并在开始时将其初始化为0。
-
**for x := 1; x <= N; x++ – **这个for循环将被用来计算奇数的和。它将从1开始运行,直到’x’的值小于或等于’N’。
- sum = sum + nextOdd – 在这里,我们通过将下一个奇数加到sum中来计算总和,直到for循环结束。
-
nextOdd = nextOdd + 2 – 这是用来计算下一个奇数的。所有奇数的差值都是2,这就是为什么我们要加2以产生下一个奇数。
-
fmt.Println(“Therefore, Sum of odd numbers up to N : “, sum) – 打印所有奇数的计算之和,直到’N’。
方法2:使用计算所有奇数之和的公式
公式
例子
- 数字=10
前10个奇数的总和 = N * N
= 10 * 10
= 100
算法
第1步 - 声明一个用于存储数字的变量-‘N’。
第2步 - 声明一个用于存储所有奇数之和的变量-‘sum’并将其初始化为0。
第3步 - 使用公式N * N计算总和,并在函数calculateSumOfOddNumbers()中把它存储在’sum’变量中。
第4步 - 打印计算出的和,即存储在变量’sum’中的值。
例子
package main
// fmt package allows us to print formatted strings
import "fmt"
func calculateSumOfOddNumbers(N int) int {
// declaring variable ‘sum’ for storing the sum of all odd numbers
var sum int = 0
// calculating the sum
sum = N * N
return sum
}
func main() {
// declaring variable ‘N’ for storing the number
var N int = 10
var sum int
fmt.Println("Program to calculate the sum of all odd numbers up to N \n")
// calling function calculateSumOfOddNumbers() for
// calculating the sum of all odd numbers
sum = calculateSumOfOddNumbers(N)
// printing the results
fmt.Println("Finding the sum of first", N,"Odd numbers")
fmt.Println("Therefore, Sum of all odd numbers up to N : ", sum)
}
输出
Program to calculate the sum of all odd numbers up to N
Finding the sum of first 10 Odd numbers
Therefore, Sum of all odd numbers up to N : 100
代码描述
- calculateSumOfOddNumbers(N int) int – 这是一个计算奇数之和的函数。该函数有一个数据类型为int的变量’N’作为参数,也有一个数据类型为int的返回值。
-
sum = N * N
-我们用这个公式计算奇数之和-N * N
。 -
return sum – 用于返回所有奇数的计算和。
-
sum = calculateSumOfOddNumbers(N) – 我们正在调用函数calculateSumOfOddNumbers(),并将计算值存储在’sum’变量中。
-
fmt.Println(“Therefore, Sum of odd numbers : “, sum) – 打印所有奇数的计算之和。
结论
这就是用两种方法计算N以内的奇数之和的全部内容。第二种方法在时间复杂性和模块化方面更好。