📌  相关文章
📜  系列1 ^ 2 + 3 ^ 2 + 5 ^ 2 +的总和。 。 。 +(2 * n – 1)^ 2

📅  最后修改于: 2021-04-23 07:38:52             🧑  作者: Mango

给定一个序列1 2 + 3 2 + 5 2 + 7 2 +。 。 。 +(2 * n – 1) 2 ,求出该序列的和。
例子:

Input : n = 4
Output : 84
Explanation : 
sum = 12 + 32 + 52 + 72
    = 1 + 9 + 25 + 49
    = 84

Input : n = 10 
Output : 1330
Explanation :
sum = 12 + 32 + 52 + 72 + 92 + 112 + 132 + 152 + 172 + 192
    = 1 + 9 + 24 + 49 + . . . + 361
    = 1330
C++
// Program to find sum of series
// 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2.
#include 
using namespace std;
 
// Function to find sum of series.
int sumOfSeries(int n)
{
    int sum = 0;
    for (int i = 1; i <= n; i++)
        sum = sum + (2 * i - 1) * (2 * i - 1);
    return sum;
}
 
// Driver code
int main()
{
    int n = 10;
 
    cout << sumOfSeries(n);
 
    return 0;
}


Java
// Program to find sum of series
// 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2.
 
import java.io.*;
 
class GFG {
    
// Function to find sum of series.
 static int sumOfSeries(int n)
{
    int sum = 0;
    for (int i = 1; i <= n; i++)
       sum = sum + (2 * i - 1) * (2 * i - 1);
    return sum;
}
 
// Driver code
  public static void  main(String[] args)
{
    int n = 10;
    System.out.println( sumOfSeries(n));   
}
     
}


Python3
# Python Program to find sum of series
# 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2.
 
import math
 
# Function to find sum of series.
def sumOfSeries(n):
 
    sum = 0
    for i in range(1,n+1):
        sum = sum + (2 * i - 1) * (2 * i - 1)
    return sum
     
# driver code
n= 10
print(sumOfSeries(n))
 
# This code is contributed by Gitanjali.


C#
// C# Program to find sum of series
// 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2.
using System;
 
class GFG {
     
// Function to find sum of series.
static int sumOfSeries(int n)
{
    int sum = 0;
    for (int i = 1; i <= n; i++)
        sum = sum + (2 * i - 1) * (2 * i - 1);
     
    return sum;
}
 
// Driver code
public static void Main()
{
    int n = 10;
    Console.Write( sumOfSeries(n));
}
}
 
/* This code is contributed by vt_m*/


PHP


Javascript


C++
// Program to find sum of series
// 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2.
#include 
using namespace std;
 
// Function that find sum of series.
int sumOfSeries(int n)
{
    // Formula to find sum of series.
    return (n * (2 * n - 1) * (2 * n + 1)) / 3;
}
 
// Driver code
int main()
{
    int n = 10;
    cout << sumOfSeries(n);
    return 0;
}


Java
// Java Program to find sum of series
// 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2.
 
import java.io.*;
import java.util.*;
 
class GFG {
     
// Function to find sum of series.
static int sumOfSeries(int n)
{
   // Formula to find sum of series.
    return (n * (2 * n - 1) * (2 * n + 1)) / 3;
 
}
 
// Driver function
   public static void main (String[] args) {
   int n=10;
    System.out.println(sumOfSeries(n));
     
}
 
}
 
// This code is contributed by Gitanjali.


Python3
# Python Program to find sum of series
# 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2.
 
import math
 
# Function to find sum of series.
def sumOfSeries(n):
 
   # Formula to find sum of series.
    return int((n * (2 * n - 1) * (2 * n + 1)) / 3)
  
# driver code
n=10
print(sumOfSeries(n))
 
# This code is contributed by Gitanjali.


C#
// C# Program to find sum of series
// 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2.
using System;
 
class GFG {
     
// Function to find sum of series.
static int sumOfSeries(int n)
{
    // Formula to find sum of series.
    return (n * (2 * n - 1) * (2 * n + 1)) / 3;
}
 
// Driver function
public static void Main ()
{
    int n = 10;
    Console.Write(sumOfSeries(n));
}
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:
1330

时间复杂度: O(n)

另一种方法:使用公式来查找序列的和:

12 + 32 + 52 + 
     72 + . . . + (2*n - 1)2 
      = (n * (2 * n - 1) * (2 * n + 1)) / 3.


Please refer sum of squares of even and odd numbers for proof.

C++

// Program to find sum of series
// 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2.
#include 
using namespace std;
 
// Function that find sum of series.
int sumOfSeries(int n)
{
    // Formula to find sum of series.
    return (n * (2 * n - 1) * (2 * n + 1)) / 3;
}
 
// Driver code
int main()
{
    int n = 10;
    cout << sumOfSeries(n);
    return 0;
}

Java

// Java Program to find sum of series
// 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2.
 
import java.io.*;
import java.util.*;
 
class GFG {
     
// Function to find sum of series.
static int sumOfSeries(int n)
{
   // Formula to find sum of series.
    return (n * (2 * n - 1) * (2 * n + 1)) / 3;
 
}
 
// Driver function
   public static void main (String[] args) {
   int n=10;
    System.out.println(sumOfSeries(n));
     
}
 
}
 
// This code is contributed by Gitanjali.

Python3

# Python Program to find sum of series
# 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2.
 
import math
 
# Function to find sum of series.
def sumOfSeries(n):
 
   # Formula to find sum of series.
    return int((n * (2 * n - 1) * (2 * n + 1)) / 3)
  
# driver code
n=10
print(sumOfSeries(n))
 
# This code is contributed by Gitanjali.

C#

// C# Program to find sum of series
// 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2.
using System;
 
class GFG {
     
// Function to find sum of series.
static int sumOfSeries(int n)
{
    // Formula to find sum of series.
    return (n * (2 * n - 1) * (2 * n + 1)) / 3;
}
 
// Driver function
public static void Main ()
{
    int n = 10;
    Console.Write(sumOfSeries(n));
}
}
 
// This code is contributed by vt_m.

的PHP


Java脚本


输出:
1330

时间复杂度: O(1)