前n个奇数的平方系列取系列中前n个奇数的平方。
系列是:1,9,25,49,81,121…
该级数也可以写为 – 12, 32, 52, 72, 9 2, 112….
这个级数的和有一个数学公式 –
n(2n+1) (2n-1)/ 3= n(4n2 – 1)/3
举个例子,
Input: N = 4Output: sum =
登录后复制
解释
12 + 32 + 52 + 72 = 1 +9+ 25 + 49 = 84
使用公式,和 = 4(4(4)2- 1)/3 = 4(64-1)/3 = 4(63)/3 = 4*21 = 84 这两种方法都是好的,但使用数学公式的方法更好,因为它不使用外观,从而减少了时间复杂度。
例子
#include int main() { int n = 8; int sum = 0; for (int i = 1; i输出
The sum of square of first 8 odd numbers is 680登录后复制
示例
#include 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登录后复制
以上就是前n个奇数的平方和的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2584571.html