Home > Backend Development > C++ > Sum sequence 1^2 + 3^2 + 5^2 + . . . + (2*n – 1)^2

Sum sequence 1^2 + 3^2 + 5^2 + . . . + (2*n – 1)^2

WBOY
Release: 2023-08-31 18:41:08
forward
1114 people have browsed it

求和序列 1^2 + 3^2 + 5^2 + . . . + (2*n – 1)^2

A series is a group of numbers that share some common characteristics that each number follows. These mathematical sequences are defined based on some mathematical logic, such as each number increasing by the same interval (arithmetic sequence), each number increasing by the same multiple (geometric sequence), and many other patterns.

To find the sum of a series, we need to evaluate the series and develop a general formula for it. But there is no common statement in this series, so we have to take the classic approach by adding each number of the series to a sum variable.

Let's take an example, this will make the logic clearer:

Sum the series up to the 7th term

sum(7) = 12 22 32 42 52 62 72 = 455

Example

#include <stdio.h>
int main() {
   int i, n, sum=0;
   n=17 ;
   for ( i = 1; i <= n; i++) {
      sum = sum + (2 * i - 1) * (2 * i - 1);
   }
   printf("The sum of series upto %d is %d", n, sum);
}
Copy after login

Output

The sum of series upto 17 is 6545
Copy after login

The above is the detailed content of Sum sequence 1^2 + 3^2 + 5^2 + . . . + (2*n – 1)^2. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template