📜  前n个自然数的立方和的程序

📅  最后修改于: 2021-04-23 21:25:50             🧑  作者: Mango

打印系列1 3 + 2 3 + 3 3 + 4 3 +…。+ n 3的总和直到第n个项。
例子 :

Input : n = 5
Output : 225
13 + 23 + 33 + 43 + 53 = 225

Input : n = 7
Output : 784
13 + 23 + 33 + 43 + 53 + 
63 + 73 = 784

一个简单的解决方案是一个接一个的添加项。

C++
// Simple C++ program to find sum of series
// with cubes of first n natural numbers
#include 
using namespace std;
 
/* Returns the sum of series */
int sumOfSeries(int n)
{
    int sum = 0;
    for (int x = 1; x <= n; x++)
        sum += x * x * x;
    return sum;
}
 
// Driver Function
int main()
{
    int n = 5;
    cout << sumOfSeries(n);
    return 0;
}


Java
// Simple Java program to find sum of series
// with cubes of first n natural numbers
 
import java.util.*;
import java.lang.*;
class GFG {
 
    /* Returns the sum of series */
    public static int sumOfSeries(int n)
    {
        int sum = 0;
        for (int x = 1; x <= n; x++)
            sum += x * x * x;
        return sum;
    }
 
    // Driver Function
    public static void main(String[] args)
    {
        int n = 5;
        System.out.println(sumOfSeries(n));
    }
}
 
// Code Contributed by Mohit Gupta_OMG <(0_o)>


Python3
# Simple Python program to find sum of series
# with cubes of first n natural numbers
 
# Returns the sum of series
def sumOfSeries(n):
    sum = 0
    for i in range(1, n + 1):
        sum += i * i*i
         
    return sum
 
  
# Driver Function
n = 5
print(sumOfSeries(n))
 
# Code Contributed by Mohit Gupta_OMG <(0_o)>


C#
// Simple C# program to find sum of series
// with cubes of first n natural numbers
using System;
 
class GFG {
    /* Returns the sum of series */
    static int sumOfSeries(int n)
    {
        int sum = 0;
        for (int x = 1; x <= n; x++)
            sum += x * x * x;
        return sum;
    }
 
    // Driver Function
    public static void Main()
    {
        int n = 5;
        Console.Write(sumOfSeries(n));
    }
}
// This code is contributed by
// Smitha Dinesh Semwal


PHP


Javascript


C++
// A formula based C++ program to find sum
// of series with cubes of first n natural
// numbers
#include 
using namespace std;
 
int sumOfSeries(int n)
{
    int x = (n * (n + 1) / 2);
    return x * x;
}
 
// Driver Function
int main()
{
    int n = 5;
    cout << sumOfSeries(n);
    return 0;
}


Java
// A formula based Java program to find sum
// of series with cubes of first n natural
// numbers
 
import java.util.*;
import java.lang.*;
class GFG {
    /* Returns the sum of series */
    public static int sumOfSeries(int n)
    {
        int x = (n * (n + 1) / 2);
 
        return x * x;
    }
 
    // Driver Function
    public static void main(String[] args)
    {
        int n = 5;
        System.out.println(sumOfSeries(n));
    }
}
 
// Code Contributed by Mohit Gupta_OMG <(0_o)>


Python3
# A formula based Python program to find sum
# of series with cubes of first n natural
# numbers
 
# Returns the sum of series
def sumOfSeries(n):
    x = (n * (n + 1)  / 2)
    return (int)(x * x)
 
 
  
# Driver Function
n = 5
print(sumOfSeries(n))
 
# Code Contributed by Mohit Gupta_OMG <(0_o)>


C#
// A formula based C# program to
// find sum of series with cubes
// of first n natural numbers
using System;
 
class GFG {
     
    // Returns the sum of series
    public static int sumOfSeries(int n)
    {
        int x = (n * (n + 1) / 2);
 
        return x * x;
    }
 
    // Driver Function
    public static void Main()
    {
        int n = 5;
         
        Console.Write(sumOfSeries(n));
    }
}
 
// Code Contributed by nitin mittal.


PHP


Javascript


C++
// Efficient CPP program to find sum of cubes
// of first n natural numbers that avoids
// overflow if result is going to be withing
// limits.
#include 
using namespace std;
 
// Returns sum of first n natural
// numbers
int sumOfSeries(int n)
{
    int x;
    if (n % 2 == 0)
        x = (n / 2) * (n + 1);
    else
        x = ((n + 1) / 2) * n;
    return x * x;
}
 
// Driver code
int main()
{
    int n = 5;
    cout << sumOfSeries(n);
    return 0;
}


Java
// Efficient Java program to find sum of cubes
// of first n natural numbers that avoids
// overflow if result is going to be withing
// limits.
import java.util.*;
import java.lang.*;
class GFG {
    /* Returns the sum of series */
    public static int sumOfSeries(int n)
    {
        int x;
        if (n % 2 == 0)
            x = (n / 2) * (n + 1);
        else
            x = ((n + 1) / 2) * n;
        return x * x;
    }
 
    // Driver Function
    public static void main(String[] args)
    {
        int n = 5;
        System.out.println(sumOfSeries(n));
    }
}
// Code Contributed by Mohit Gupta_OMG <(0_o)>


Python3
# Efficient Python program to find sum of cubes
# of first n natural numbers that avoids
# overflow if result is going to be withing
# limits.
 
# Returns the sum of series
def sumOfSeries(n):
    x = 0
    if n % 2 == 0 :
        x = (n / 2) * (n + 1)
    else:
        x = ((n + 1) / 2) * n
         
    return (int)(x * x)
 
  
# Driver Function
n = 5
print(sumOfSeries(n))
 
# Code Contributed by Mohit Gupta_OMG <(0_o)>


C#
// Efficient C# program to find sum of
// cubes of first n natural numbers
// that avoids overflow if result is
// going to be withing limits.
using System;
 
class GFG {
     
    /* Returns the sum of series */
    public static int sumOfSeries(int n)
    {
        int x;
        if (n % 2 == 0)
            x = (n / 2) * (n + 1);
        else
            x = ((n + 1) / 2) * n;
        return x * x;
    }
     
    // Driver code
    static public void Main ()
    {
        int n = 5;
        Console.WriteLine(sumOfSeries(n));
    }
}
 
// This code is contributed by Ajit.


PHP


Javascript


输出 :

225

时间复杂度:O(n)
一个有效的解决方案是使用直接数学公式,该公式为(n(n + 1)/ 2)^ 2

For n = 5 sum by formula is
       (5*(5 + 1 ) / 2)) ^ 2
          = (5*6/2) ^ 2
          = (15) ^ 2
          = 225

For n = 7, sum by formula is
       (7*(7 + 1 ) / 2)) ^ 2
          = (7*8/2) ^ 2
          = (28) ^ 2
          = 784

C++

// A formula based C++ program to find sum
// of series with cubes of first n natural
// numbers
#include 
using namespace std;
 
int sumOfSeries(int n)
{
    int x = (n * (n + 1) / 2);
    return x * x;
}
 
// Driver Function
int main()
{
    int n = 5;
    cout << sumOfSeries(n);
    return 0;
}

Java

// A formula based Java program to find sum
// of series with cubes of first n natural
// numbers
 
import java.util.*;
import java.lang.*;
class GFG {
    /* Returns the sum of series */
    public static int sumOfSeries(int n)
    {
        int x = (n * (n + 1) / 2);
 
        return x * x;
    }
 
    // Driver Function
    public static void main(String[] args)
    {
        int n = 5;
        System.out.println(sumOfSeries(n));
    }
}
 
// Code Contributed by Mohit Gupta_OMG <(0_o)>

Python3

# A formula based Python program to find sum
# of series with cubes of first n natural
# numbers
 
# Returns the sum of series
def sumOfSeries(n):
    x = (n * (n + 1)  / 2)
    return (int)(x * x)
 
 
  
# Driver Function
n = 5
print(sumOfSeries(n))
 
# Code Contributed by Mohit Gupta_OMG <(0_o)>

C#

// A formula based C# program to
// find sum of series with cubes
// of first n natural numbers
using System;
 
class GFG {
     
    // Returns the sum of series
    public static int sumOfSeries(int n)
    {
        int x = (n * (n + 1) / 2);
 
        return x * x;
    }
 
    // Driver Function
    public static void Main()
    {
        int n = 5;
         
        Console.Write(sumOfSeries(n));
    }
}
 
// Code Contributed by nitin mittal.

的PHP


Java脚本


输出:

225

时间复杂度:O(1)
这个公式如何运作?
我们可以使用数学归纳法证明该公式。我们可以很容易地看到,该公式对于n = 1和n = 2成立。对于n = k-1,使它成立。

Let the formula be true for n = k-1.
Sum of first (k-1) natural numbers = 
            [((k - 1) * k)/2]2

Sum of first k natural numbers = 
          = Sum of (k-1) numbers + k3
          = [((k - 1) * k)/2]2 + k3
          = [k2(k2 - 2k + 1) + 4k3]/4
          = [k4 + 2k3 + k2]/4
          = k2(k2 + 2k + 1)/4
          = [k*(k+1)/2]2

即使结果未超出整数限制,上述程序也会导致溢出。像以前的帖子一样,我们可以通过先进行除法来在一定程度上避免溢出。

C++

// Efficient CPP program to find sum of cubes
// of first n natural numbers that avoids
// overflow if result is going to be withing
// limits.
#include 
using namespace std;
 
// Returns sum of first n natural
// numbers
int sumOfSeries(int n)
{
    int x;
    if (n % 2 == 0)
        x = (n / 2) * (n + 1);
    else
        x = ((n + 1) / 2) * n;
    return x * x;
}
 
// Driver code
int main()
{
    int n = 5;
    cout << sumOfSeries(n);
    return 0;
}

Java

// Efficient Java program to find sum of cubes
// of first n natural numbers that avoids
// overflow if result is going to be withing
// limits.
import java.util.*;
import java.lang.*;
class GFG {
    /* Returns the sum of series */
    public static int sumOfSeries(int n)
    {
        int x;
        if (n % 2 == 0)
            x = (n / 2) * (n + 1);
        else
            x = ((n + 1) / 2) * n;
        return x * x;
    }
 
    // Driver Function
    public static void main(String[] args)
    {
        int n = 5;
        System.out.println(sumOfSeries(n));
    }
}
// Code Contributed by Mohit Gupta_OMG <(0_o)>

Python3

# Efficient Python program to find sum of cubes
# of first n natural numbers that avoids
# overflow if result is going to be withing
# limits.
 
# Returns the sum of series
def sumOfSeries(n):
    x = 0
    if n % 2 == 0 :
        x = (n / 2) * (n + 1)
    else:
        x = ((n + 1) / 2) * n
         
    return (int)(x * x)
 
  
# Driver Function
n = 5
print(sumOfSeries(n))
 
# Code Contributed by Mohit Gupta_OMG <(0_o)>

C#

// Efficient C# program to find sum of
// cubes of first n natural numbers
// that avoids overflow if result is
// going to be withing limits.
using System;
 
class GFG {
     
    /* Returns the sum of series */
    public static int sumOfSeries(int n)
    {
        int x;
        if (n % 2 == 0)
            x = (n / 2) * (n + 1);
        else
            x = ((n + 1) / 2) * n;
        return x * x;
    }
     
    // Driver code
    static public void Main ()
    {
        int n = 5;
        Console.WriteLine(sumOfSeries(n));
    }
}
 
// This code is contributed by Ajit.

的PHP


Java脚本


输出:

225