📜  前N个自然数的交替符号平方和

📅  最后修改于: 2021-05-07 07:23:45             🧑  作者: Mango

给定数字N,任务是找到前N个自然数的交替符号平方和,即

例子:

Input: N = 2
Output: 5
Explanation:
Required sum = 12 - 22 = -1

Input: N = 8
Output: 36
Explanation:
Required sum 
= 12 - 22 + 32 - 42 + 52 - 62 + 72 - 82 
= 36

天真的方法:O(N)
解决这一问题的朴素或蛮力方法指出要找到从1到N的每个数字的平方,并用交替的符号相加以获得所需的总和。

  1. 对于1到N中的每个数字,找到其平方
  2. 将这些正方形加上交替的符号
  3. 这将给出所需的总和。

下面是上述方法的实现:

C++
// C++ program to find Sum of alternating
// sign Squares of first N natural numbers
 
#include 
using namespace std;
 
// Function to calculate
// the alternating sign sum
int summation(int n)
{
 
    // Variable to store the sum
    int sum = 0;
 
    // Loop to iterate each number
    // from 1 to N
    for (int i = 1; i <= n; i++) {
 
        // The alternating sign is put
        // by checking if the number
        // is even or odd
        if (i % 2 == 1)
            // Add the square with the sign
            sum += (i * i);
 
        else
            // Add the square with the sign
            sum -= (i * i);
    }
    return sum;
}
 
// Driver code
int main()
{
    int N = 2;
    cout << summation(N);
    return 0;
}


Java
// Java program to find Sum of alternating
// sign Squares of first N natural numbers
class GFG
{
         
    // Function to calculate
    // the alternating sign sum
    static int summation(int n)
    {
     
        // Variable to store the sum
        int sum = 0;
     
        // Loop to iterate each number
        // from 1 to N
        for (int i = 1; i <= n; i++) {
     
            // The alternating sign is put
            // by checking if the number
            // is even or odd
            if (i % 2 == 1)
 
                // Add the square with the sign
                sum += (i * i);
     
            else
 
                // Add the square with the sign
                sum -= (i * i);
        }
        return sum;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int N = 2;
        System.out.println(summation(N));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 program to find Sum of alternating
# sign Squares of first N natural numbers
 
# Function to calculate
# the alternating sign sum
def summation(n) :
 
    # Variable to store the sum
    sum = 0;
 
    # Loop to iterate each number
    # from 1 to N
    for i in range(1, n + 1) :
 
        # The alternating sign is put
        # by checking if the number
        # is even or odd
        if (i % 2 == 1) :
            # Add the square with the sign
            sum += (i * i);
 
        else :
            # Add the square with the sign
            sum -= (i * i);
     
    return sum;
 
 
# Driver code
if __name__ == "__main__" :
 
    N = 2;
    print(summation(N));
 
    # This code is contributed by AnkitRai01


C#
// C# program to find Sum of alternating
// sign Squares of first N natural numbers
using System;
 
class GFG
{
         
    // Function to calculate
    // the alternating sign sum
    static int summation(int n)
    {
     
        // Variable to store the sum
        int sum = 0;
     
        // Loop to iterate each number
        // from 1 to N
        for (int i = 1; i <= n; i++) {
     
            // The alternating sign is put
            // by checking if the number
            // is even or odd
            if (i % 2 == 1)
 
                // Add the square with the sign
                sum += (i * i);
     
            else
 
                // Add the square with the sign
                sum -= (i * i);
        }
        return sum;
    }
     
    // Driver code
    public static void Main()
    {
        int N = 2;
        Console.WriteLine(summation(N));
    }
}
 
// This code is contributed by AnkitRai01


Javascript


C++
// C++ program to find Sum of alternating
// sign Squares of first N natural numbers
 
#include 
using namespace std;
 
// Function to calculate
// the alternating sign sum
int summation(int n)
{
 
    // Variable to store the absolute sum
    int abs_sum = n * (n + 1) / 2;
 
    // Variable to store the sign
    int sign = n + 1 % 2 == 0 ? 1 : -1;
 
    // Variable to store the resultant sum
    int result_sum = sign * abs_sum;
 
    return result_sum;
}
 
// Driver code
int main()
{
    int N = 2;
    cout << summation(N);
    return 0;
}


Java
// Java program to find Sum of alternating
// sign Squares of first N natural numbers
class GFG
{
     
    // Function to calculate
    // the alternating sign sum
    static int summation(int n)
    {
     
        // Variable to store the absolute sum
        int abs_sum = n * (n + 1) / 2;
     
        // Variable to store the sign
        int sign = n + 1 % 2 == 0 ? 1 : -1;
     
        // Variable to store the resultant sum
        int result_sum = sign * abs_sum;
     
        return result_sum;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int N = 2;
        System.out.println(summation(N));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 program to find Sum of alternating
# sign Squares of first N natural numbers
 
# Function to calculate
# the alternating sign sum
def summation(n) :
 
    # Variable to store the absolute sum
    abs_sum = n * (n + 1) // 2;
 
    # Variable to store the sign
    sign = 1 if ((n + 1) % 2 == 0 ) else -1;
 
    # Variable to store the resultant sum
    result_sum = sign * abs_sum;
 
    return result_sum;
 
# Driver code
if __name__ == "__main__" :
 
    N = 2;
    print(summation(N));
 
# This code is contributed by AnkitRai01


C#
// C# program to find Sum of alternating
// sign Squares of first N natural numbers
 
using System;
 
public class GFG
{
     
    // Function to calculate
    // the alternating sign sum
    static int summation(int n)
    {
     
        // Variable to store the absolute sum
        int abs_sum = (int)(n * (n + 1) / 2);
     
        // Variable to store the sign
        int sign = n + 1 % 2 == 0 ? 1 : -1;
     
        // Variable to store the resultant sum
        int result_sum = sign * abs_sum;
     
        return result_sum;
    }
     
    // Driver code
    public static void Main()
    {
        int N = 2;
        Console.WriteLine(summation(N));
    }
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
-3

有效方法:O(1)
有一个公式可以找到带有交替符号的前n个数字的平方和:

    \[\LARGE 1^{2}-2^{2}+3^{2}-4^{2}+... = (-1)^{n+1} \text{ } \frac{n(n+1)}{2}\]

这是如何运作的?

We can prove this formula using induction.
We can easily see that the formula is true for
n = 1 and n = 2 as sums are 1 and -3 respectively.

Let it be true for n = k-1. So sum of k-1 numbers
is (-1)k(k - 1) * k / 2

In the following steps, we show that it is true 
for k assuming that it is true for k-1.


Sum of k numbers
 =(-1)k (Sum of k-1 numbers + k2)
 =(-1)k+1 ((k - 1) * k / 2 + k2)
 =(-1)k+1 (k * (k + 1) / 2), which is true.

因此,为了找到前N个自然数的交替符号平方和,只需计算公式即可\frac{n(n+1)}{2}    并打印结果。

C++

// C++ program to find Sum of alternating
// sign Squares of first N natural numbers
 
#include 
using namespace std;
 
// Function to calculate
// the alternating sign sum
int summation(int n)
{
 
    // Variable to store the absolute sum
    int abs_sum = n * (n + 1) / 2;
 
    // Variable to store the sign
    int sign = n + 1 % 2 == 0 ? 1 : -1;
 
    // Variable to store the resultant sum
    int result_sum = sign * abs_sum;
 
    return result_sum;
}
 
// Driver code
int main()
{
    int N = 2;
    cout << summation(N);
    return 0;
}

Java

// Java program to find Sum of alternating
// sign Squares of first N natural numbers
class GFG
{
     
    // Function to calculate
    // the alternating sign sum
    static int summation(int n)
    {
     
        // Variable to store the absolute sum
        int abs_sum = n * (n + 1) / 2;
     
        // Variable to store the sign
        int sign = n + 1 % 2 == 0 ? 1 : -1;
     
        // Variable to store the resultant sum
        int result_sum = sign * abs_sum;
     
        return result_sum;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int N = 2;
        System.out.println(summation(N));
    }
}
 
// This code is contributed by AnkitRai01

Python3

# Python3 program to find Sum of alternating
# sign Squares of first N natural numbers
 
# Function to calculate
# the alternating sign sum
def summation(n) :
 
    # Variable to store the absolute sum
    abs_sum = n * (n + 1) // 2;
 
    # Variable to store the sign
    sign = 1 if ((n + 1) % 2 == 0 ) else -1;
 
    # Variable to store the resultant sum
    result_sum = sign * abs_sum;
 
    return result_sum;
 
# Driver code
if __name__ == "__main__" :
 
    N = 2;
    print(summation(N));
 
# This code is contributed by AnkitRai01

C#

// C# program to find Sum of alternating
// sign Squares of first N natural numbers
 
using System;
 
public class GFG
{
     
    // Function to calculate
    // the alternating sign sum
    static int summation(int n)
    {
     
        // Variable to store the absolute sum
        int abs_sum = (int)(n * (n + 1) / 2);
     
        // Variable to store the sign
        int sign = n + 1 % 2 == 0 ? 1 : -1;
     
        // Variable to store the resultant sum
        int result_sum = sign * abs_sum;
     
        return result_sum;
    }
     
    // Driver code
    public static void Main()
    {
        int N = 2;
        Console.WriteLine(summation(N));
    }
}
 
// This code is contributed by AnkitRai01

Java脚本


输出:
-3