📌  相关文章
📜  查找系列1 ^ 2 – 2 ^ 2 + 3 ^ 2 – 4 ^ 2……最多n个项的总和

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

给定数字n,任务是找到以下序列的和,直到n个项:

例子:

Input: n = 2
Output: -3
Explanation: 
    sum = 12 - 22
    = 1 - 4
    = -3

Input: n = 3
Output: 6
Explanation: 
    sum = 12 - 22 + 32
    = 1 - 4 + 9
    = 6

天真的方法:

此方法涉及简单地运行从1到n的i循环,如果i为奇数,则将其平方加到结果中,i为偶数,然后简单地将其平方减去结果。
下面是上述方法的实现:

C++
// C++ program to find sum of series
// 1^2 - 2^2 + 3^3 - 4^4 + ...
 
#include 
using namespace std;
 
// Function to find sum of series
int sum_of_series(int n)
{
    int result = 0;
    for (int i = 1; i <= n; i++) {
 
        // If i is even
        if (i % 2 == 0)
            result = result - pow(i, 2);
 
        // If i is odd
        else
            result = result + pow(i, 2);
    }
 
    // return the result
    return result;
}
 
// Driver Code
int main(void)
{
 
    // Get n
    int n = 3;
 
    // Find the sum
    cout << sum_of_series(n) << endl;
 
    // Get n
    n = 10;
 
    // Find the sum
    cout << sum_of_series(n) << endl;
}


Java
// Java Program to find sum of series
// 1^2 - 2^2 + 3^3 - 4^4 + ...
import java.util.*;
import java.lang.*;
 
class GFG
{
// Function to find sum of series
static int sum_of_series(int n)
{
    int result = 0;
    for (int i = 1; i <= n; i++)
    {
 
        // If i is even
        if (i % 2 == 0)
            result = result -
                    (int)Math.pow(i, 2);
 
        // If i is odd
        else
            result = result +
                    (int)Math.pow(i, 2);
    }
 
    // return the result
    return result;
}
 
// Driver Code
public static void main(String args[])
{
 
    // Get n
    int n = 3;
 
    // Find the sum
    System.out.println(sum_of_series(n));
 
    // Get n
    n = 10;
 
    // Find the sum
    System.out.println(sum_of_series(n));
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


Python3
# Python3 program to find sum of series
# 1^2 - 2^2 + 3^3 - 4^4 + ...
 
# Function to find sum of series
def sum_of_series(n):
 
    result = 0
    for i in range(1, n + 1) :
 
        # If i is even
        if (i % 2 == 0):
            result = result - pow(i, 2)
 
        # If i is odd
        else:
            result = result + pow(i, 2)
 
    # return the result
    return result
 
# Driver Code
if __name__ == "__main__":
 
    # Get n
    n = 3
 
    # Find the sum
    print(sum_of_series(n))
 
    # Get n
    n = 10
 
    # Find the sum
    print(sum_of_series(n))
 
# This code is contributed
# by ChitraNayal


C#
// C# Program to find sum of series
// 1^2 - 2^2 + 3^3 - 4^4 + ...
using System;
 
class GFG
{
// Function to find sum of series
static int sum_of_series(int n)
{
    int result = 0;
    for (int i = 1; i <= n; i++)
    {
 
        // If i is even
        if (i % 2 == 0)
            result = result -
                    (int)Math.Pow(i, 2);
 
        // If i is odd
        else
            result = result +
                    (int)Math.Pow(i, 2);
    }
 
    // return the result
    return result;
}
 
// Driver Code
public static void Main()
{
 
    // Get n
    int n = 3;
 
    // Find the sum
    Console.WriteLine(sum_of_series(n));
 
    // Get n
    n = 10;
 
    // Find the sum
    Console.WriteLine(sum_of_series(n));
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


PHP


Javascript


C++
// C++ Program to find sum of series
// 1^2 - 2^2 +3^3 -4^4 + ...
 
#include 
using namespace std;
 
// Function to find sum of series
int sum_of_series(int n)
{
    int result = 0;
 
    // If n is even
    if (n % 2 == 0) {
        result = -(n * (n + 1)) / 2;
    }
 
    // If n is odd
    else {
        result = (n * (n + 1)) / 2;
    }
 
    // return the result
    return result;
}
 
// Driver Code
int main(void)
{
 
    // Get n
    int n = 3;
 
    // Find the sum
    cout << sum_of_series(n) << endl;
 
    // Get n
    n = 10;
 
    // Find the sum
    cout << sum_of_series(n) << endl;
}


Java
// Java Program to find sum of series
// 1^2 - 2^2 +3^3 -4^4 + ...
import java.util.*;
import java.lang.*;
 
class GFG
{
// Function to find sum of series
static int sum_of_series(int n)
{
    int result = 0;
 
    // If n is even
    if (n % 2 == 0)
    {
        result = -(n * (n + 1)) / 2;
    }
 
    // If n is odd
    else
    {
        result = (n * (n + 1)) / 2;
    }
 
    // return the result
    return result;
}
 
// Driver Code
public static void main(String args[])
{
 
    // Get n
    int n = 3;
 
    // Find the sum
    System.out.println(sum_of_series(n));
 
    // Get n
    n = 10;
 
    // Find the sum
    System.out.println(sum_of_series(n));
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


Python3
# Python3 Program to find sum of series
# 1^2 - 2^2 +3^3 -4^4 + ...
 
# Function to find sum of series
def sum_of_series(n) :
 
    result = 0
 
    # If n is even
    if (n % 2 == 0) :
        result = -(n * (n + 1)) // 2
     
    # If n is odd
    else :
        result = (n * (n + 1)) // 2
     
    # return the result
    return result
 
# Driver Code
if __name__ == "__main__" :
 
    # Get n
    n = 3
 
    # Find the sum
    print(sum_of_series(n))
 
    # Get n
    n = 10
 
    # Find the sum
    print(sum_of_series(n))
 
# This code is contributed by Ryuga


C#
// C# Program to find sum of series
// 1^2 - 2^2 +3^3 -4^4 + ...
 
using System;
 
class GFG
{
// Function to find sum of series
static int sum_of_series(int n)
{
    int result = 0;
 
    // If n is even
    if (n % 2 == 0)
    {
        result = -(n * (n + 1)) / 2;
    }
 
    // If n is odd
    else
    {
        result = (n * (n + 1)) / 2;
    }
 
    // return the result
    return result;
}
 
// Driver Code
public static void Main()
{
 
    // Get n
    int n = 3;
 
    // Find the sum
    Console.WriteLine(sum_of_series(n));
 
    // Get n
    n = 10;
 
    // Find the sum
    Console.WriteLine(sum_of_series(n));
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


PHP


Javascript


输出:
6
-55

时间复杂度:上述代码的复杂度为O(n)。

高效方法

它基于n的条件
如果n是偶数:

如果n为奇数:

下面是上述方法的实现:

C++

// C++ Program to find sum of series
// 1^2 - 2^2 +3^3 -4^4 + ...
 
#include 
using namespace std;
 
// Function to find sum of series
int sum_of_series(int n)
{
    int result = 0;
 
    // If n is even
    if (n % 2 == 0) {
        result = -(n * (n + 1)) / 2;
    }
 
    // If n is odd
    else {
        result = (n * (n + 1)) / 2;
    }
 
    // return the result
    return result;
}
 
// Driver Code
int main(void)
{
 
    // Get n
    int n = 3;
 
    // Find the sum
    cout << sum_of_series(n) << endl;
 
    // Get n
    n = 10;
 
    // Find the sum
    cout << sum_of_series(n) << endl;
}

Java

// Java Program to find sum of series
// 1^2 - 2^2 +3^3 -4^4 + ...
import java.util.*;
import java.lang.*;
 
class GFG
{
// Function to find sum of series
static int sum_of_series(int n)
{
    int result = 0;
 
    // If n is even
    if (n % 2 == 0)
    {
        result = -(n * (n + 1)) / 2;
    }
 
    // If n is odd
    else
    {
        result = (n * (n + 1)) / 2;
    }
 
    // return the result
    return result;
}
 
// Driver Code
public static void main(String args[])
{
 
    // Get n
    int n = 3;
 
    // Find the sum
    System.out.println(sum_of_series(n));
 
    // Get n
    n = 10;
 
    // Find the sum
    System.out.println(sum_of_series(n));
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)

Python3

# Python3 Program to find sum of series
# 1^2 - 2^2 +3^3 -4^4 + ...
 
# Function to find sum of series
def sum_of_series(n) :
 
    result = 0
 
    # If n is even
    if (n % 2 == 0) :
        result = -(n * (n + 1)) // 2
     
    # If n is odd
    else :
        result = (n * (n + 1)) // 2
     
    # return the result
    return result
 
# Driver Code
if __name__ == "__main__" :
 
    # Get n
    n = 3
 
    # Find the sum
    print(sum_of_series(n))
 
    # Get n
    n = 10
 
    # Find the sum
    print(sum_of_series(n))
 
# This code is contributed by Ryuga

C#

// C# Program to find sum of series
// 1^2 - 2^2 +3^3 -4^4 + ...
 
using System;
 
class GFG
{
// Function to find sum of series
static int sum_of_series(int n)
{
    int result = 0;
 
    // If n is even
    if (n % 2 == 0)
    {
        result = -(n * (n + 1)) / 2;
    }
 
    // If n is odd
    else
    {
        result = (n * (n + 1)) / 2;
    }
 
    // return the result
    return result;
}
 
// Driver Code
public static void Main()
{
 
    // Get n
    int n = 3;
 
    // Find the sum
    Console.WriteLine(sum_of_series(n));
 
    // Get n
    n = 10;
 
    // Find the sum
    Console.WriteLine(sum_of_series(n));
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)

的PHP


Java脚本


输出:
6
-55