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

📅  最后修改于: 2022-05-13 01:58:09.566000             🧑  作者: Mango

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

打印系列 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
Java


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)>


Java


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)>


Java


Java
// Efficient Java program to find sum of cubes
// of first n natural numbers that avoids
// overflow if result is going to be within
// 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)>


输出 :

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

Java

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)>

输出:

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

上面的程序会导致溢出,即使结果没有超出整数限制。和上一篇一样,我们可以通过先除法在一定程度上避免溢出。

Java

Java

// Efficient Java program to find sum of cubes
// of first n natural numbers that avoids
// overflow if result is going to be within
// 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)>

输出:

225

有关详细信息,请参阅关于前 n 个自然数的立方和的完整文章!