📜  前N个自然数的交替正负号立方的总和

📅  最后修改于: 2021-05-06 09:54:51             🧑  作者: Mango

给定数字N ,任务是找到前N个自然数的交替正负号立方的总和,即

例子:

天真的方法:一个简单的解决方案是通过在从到N的循环上进行迭代来解决此问题,并通过每次交替符号来计算总和。
下面是上述方法的实现:

C++
// C++ implementation to compute
// the sum of cubes with
// alternating sign
 
#include 
 
using namespace std;
 
// Function to compute sum
// of the cubes with
// alternating sign
int summation(int n)
{
    int sum = 0;
    for (int i = 1; i <= n; i++)
        if (i % 2 == 1)
            sum += (i * i * i);
        else
            sum -= (i * i * i);
 
    return sum;
}
 
// Driver code
int main()
{
    int n = 3;
    cout << summation(n);
    return 0;
}


Java
// Java implementation to compute
// the sum of cubes with
// alternating sign
import java.util.*;
 
class GFG {
 
// Function to compute sum
// of the cubes with
// alternating sign
static int summation(int n)
{
    int sum = 0;
     
    for(int i = 1; i <= n; i++)
    {
       if (i % 2 == 1)
           sum += (i * i * i);
       else
           sum -= (i * i * i);
    }
    return sum;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 3;
 
    System.out.println(summation(n));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 implementation to 
# compute the sum of cubes 
# with alternating sign
 
# Function to compute sum
# of the cubes with
# alternating sign
def summation(n):
     
    sum = 0
    for i in range(1, n + 1):
        if i % 2 == 1:
            sum = sum + (i * i * i)
        else:
            sum = sum - (i * i * i)
 
    return sum
 
# Driver code
n = 3
 
print(summation(n))
 
# This code is contributed by ishayadav181


C#
// C# implementation to compute
// the sum of cubes with
// alternating sign
using System;
class GFG{
 
// Function to compute sum
// of the cubes with
// alternating sign
static int summation(int n)
{
    int sum = 0;
     
    for(int i = 1; i <= n; i++)
    {
        if (i % 2 == 1)
            sum += (i * i * i);
        else
            sum -= (i * i * i);
    }
    return sum;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 3;
 
    Console.WriteLine(summation(n));
}
}
 
// This code is contributed by sapnasingh4991


Javascript


C++
// C++ implementation to compute
// the sum of cubes with
// alternating sign
 
#include 
using namespace std;
 
// Function to compute sum
// of the cubes with alternating sign
int summation(int N)
{
    int co = (N + 1) / 2;
    int ce = (N) / 2;
    int se = 2 * ((ce * (ce + 1))
                  * (ce * (ce + 1)));
    int so = (co * co)
             * (2 * ((co * co)) - 1);
    return so - se;
}
 
// Driver Code
int main()
{
    int n = 3;
    cout << summation(n);
    return 0;
}


Java
// Java implementation to compute
// the sum of cubes with
// alternating sign
import java.util.*;
 
class GFG{
 
// Function to compute sum
// of the cubes with
// alternating sign
static int summation(int N)
{
    int co = (N + 1) / 2;
    int ce = (N) / 2;
     
    int se = 2 * ((ce * (ce + 1)) *
                  (ce * (ce + 1)));
    int so = (co * co) * (2 * ((co * co)) - 1);
     
    return so - se;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 3;
 
    System.out.println(summation(n));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 implementation to compute
# the sum of cubes with
# alternating sign
 
# Function to compute sum of
# the cubes with alternating sign
def summation(N):
     
    co = (N + 1) / 2
    co = int(co)
     
    ce = N / 2
    ce = int(ce)
     
    se = 2 * ((ce * (ce + 1)) *
              (ce * (ce + 1)))
    so = (co * co) * (2 * (co * co) - 1)
     
    return so - se
 
# Driver Code
n = 3
 
print(summation(n))
 
# This code is contributed by ishayadav181


C#
// C# implementation to compute
// the sum of cubes with
// alternating sign
using System;
 
class GFG{
 
// Function to compute sum
// of the cubes with
// alternating sign
static int summation(int N)
{
    int co = (N + 1) / 2;
    int ce = (N) / 2;
     
    int se = 2 * ((ce * (ce + 1)) *
                  (ce * (ce + 1)));
    int so = (co * co) * (2 * ((co * co)) - 1);
     
    return so - se;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 3;
 
    Console.WriteLine(summation(n));
}
}
 
// This code is contributed by Rohit_ranjan


Javascript


输出:
20

高效的方法:问题中的关键发现是每个偶数都带有负号,即用于减少总和。因此,如果我们分别计算偶数和奇数的立方和,那么总和就很容易计算出来。

  • 前N个自然数中的偶数或奇数计数
    => Count(C_o) = \frac{N+1}{2}
    => Count(C_e) = \frac{N}{2}
  • 第一个偶数项的总和
    => Sum(S_e) = 2*(C_e*(C_e + 1))^{2}
  • 前奇数项之和
    => Sum(S_o) = C_o^{2} * (2*C_o^{2} - 1)
  • 总和
    => S = S_o - S_e
    => S = C_o^{2} * (2*C_o^{2} - 1) - 2*(C_e*(C_e + 1))^{2}

下面是上述方法的实现:

C++

// C++ implementation to compute
// the sum of cubes with
// alternating sign
 
#include 
using namespace std;
 
// Function to compute sum
// of the cubes with alternating sign
int summation(int N)
{
    int co = (N + 1) / 2;
    int ce = (N) / 2;
    int se = 2 * ((ce * (ce + 1))
                  * (ce * (ce + 1)));
    int so = (co * co)
             * (2 * ((co * co)) - 1);
    return so - se;
}
 
// Driver Code
int main()
{
    int n = 3;
    cout << summation(n);
    return 0;
}

Java

// Java implementation to compute
// the sum of cubes with
// alternating sign
import java.util.*;
 
class GFG{
 
// Function to compute sum
// of the cubes with
// alternating sign
static int summation(int N)
{
    int co = (N + 1) / 2;
    int ce = (N) / 2;
     
    int se = 2 * ((ce * (ce + 1)) *
                  (ce * (ce + 1)));
    int so = (co * co) * (2 * ((co * co)) - 1);
     
    return so - se;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 3;
 
    System.out.println(summation(n));
}
}
 
// This code is contributed by offbeat

Python3

# Python3 implementation to compute
# the sum of cubes with
# alternating sign
 
# Function to compute sum of
# the cubes with alternating sign
def summation(N):
     
    co = (N + 1) / 2
    co = int(co)
     
    ce = N / 2
    ce = int(ce)
     
    se = 2 * ((ce * (ce + 1)) *
              (ce * (ce + 1)))
    so = (co * co) * (2 * (co * co) - 1)
     
    return so - se
 
# Driver Code
n = 3
 
print(summation(n))
 
# This code is contributed by ishayadav181

C#

// C# implementation to compute
// the sum of cubes with
// alternating sign
using System;
 
class GFG{
 
// Function to compute sum
// of the cubes with
// alternating sign
static int summation(int N)
{
    int co = (N + 1) / 2;
    int ce = (N) / 2;
     
    int se = 2 * ((ce * (ce + 1)) *
                  (ce * (ce + 1)));
    int so = (co * co) * (2 * ((co * co)) - 1);
     
    return so - se;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 3;
 
    Console.WriteLine(summation(n));
}
}
 
// This code is contributed by Rohit_ranjan

Java脚本


输出:
20