前n个奇数的平方和
前n个奇数的平方的序列取串联的前n个奇数的平方。
该系列是:1,9,25,49,81,121…
该系列也可写为-12,32,52,72,92,112...。
该系列的总和具有一个数学公式-
N(2N+1)(2N-1)/3=N(4N2-1)/3
让我们举个例子
Input: N = 4 Output: sum =
说明
12+32+52+72=1+9+25+49=84
使用式,总和=4(4(4)21)/3=4(64-1)/3=4(63)/3=4*21=84这两种方法都很好,但使用的数学公式的一个更好,因为它不使用外观,从而降低了时间复杂度。
示例
#include <stdio.h>
int main() {
int n = 8;
int sum = 0;
for (int i = 1; i <= n; i++)
sum += (2*i - 1) * (2*i - 1);
printf("The sum of square of first %d odd numbers is %d",n, sum);
return 0;
}输出结果
The sum of square of first 8 odd numbers is 680
示例
#include <stdio.h>
int main() {
int n = 18;
int sum = ((n*((4*n*n)-1))/3);
printf("The sum of square of first %d odd numbers is %d",n, sum);
return 0;
}输出结果
The sum of square of first 18 odd numbers is 7770
热门推荐
10 读书祝福语女生短句简短
11 感谢同事新婚祝福语简短
12 感谢教官文案祝福语简短
13 员工聚会蛋糕祝福语简短
14 元宵发客户祝福语简短
15 给团队信心祝福语简短
16 医美的祝福语大全简短
17 异国姻缘的祝福语简短
18 兄妹拍照祝福语简短的话