📜  前n个自然数的四次幂和

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

编写一个程序,以找到前n个自然数1 4 + 2 4 + 3 4 + 4 4 +…。+ n 4的四次幂之和,直到第n个项。
例子 :

Input  : 4
Output : 354
14 + 24 + 34 + 44 = 354

Input  : 6
Output : 2275
14 + 24 + 34 + 44+ 54+ 64 = 2275

天真的方法:-简单地找到前n个自然数的第四次幂就是从1到n时间循环。例如假设n = 4。
(1 * 1 * 1 * 1)+(2 * 2 * 2 * 2)+(3 * 3 * 3 * 3)+(4 * 4 * 4 * 4)= 354

C++
// CPP Program to find the sum of forth powers
// of first n natural numbers
#include 
using namespace std;
 
// Return the sum of forth power of first n
// natural numbers
long long int fourthPowerSum(int n)
{
    long long int sum = 0;
    for (int i = 1; i <= n; i++)
        sum = sum + (i * i * i * i);
    return sum;
}
 
// Driven Program
int main()
{
    int n = 6;
    cout << fourthPowerSum(n) << endl;
    return 0;
}


Java
// Java Program to find the
// sum of forth powers of
// first n natural numbers
import java.io.*;
import java.util.*;
 
class GFG {
     
    // Return the sum of forth
    // power of first n natural
    // numbers
    static long fourthPowerSum(int n)
    {
        long sum = 0;
         
        for (int i = 1; i <= n; i++)
            sum = sum + (i * i * i * i);
         
        return sum;
    }
     
    public static void main (String[] args)
    {
        int n = 6;
        System.out.println(fourthPowerSum(n));
     
    }
}
 
// This code is contributed by Gitanjali.


Python3
# Python3 Program to find the
# sum of forth powers of first
# n natural numbers
import math
 
# Return the sum of forth power of
# first n natural numbers
def fourthPowerSum( n):
 
    sum = 0
    for i in range(1, n+1) :
        sum = sum + (i * i * i * i)
    return sum
# Driver method
n=6
print (fourthPowerSum(n))
 
# This code is contributed by Gitanjali.


C#
// C# program to find the
// sum of forth powers of
// first n natural numbers
using System;
 
class GFG {
     
    // Return the sum of forth power
    // of first n natural numbers
    static long fourthPowerSum(int n)
    {
        long sum = 0;
         
        for (int i = 1; i <= n; i++)
            sum = sum + (i * i * i * i);
         
        return sum;
    }
     
    public static void Main ()
    {
        int n = 6;
        Console.WriteLine(fourthPowerSum(n));
     
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// CPP Program to find the sum of forth power of first
// n natural numbers
#include 
using namespace std;
 
// Return the sum of forth power of first n natural
// numbers
long long int fourthPowerSum(int n)
{
    return ((6 * n * n * n * n * n) +
            (15 * n * n * n * n) +
            (10 * n * n * n) - n) / 30;
}
 
// Driven Program
int main()
{
    int n = 6;
    cout << fourthPowerSum(n) << endl;
    return 0;
}


Java
// Java Program to find the
// sum of forth powers of
// first n natural numbers
import java.io.*;
import java.util.*;
 
class GFG {
     
    // Return the sum of
    // forth power of first
    // n natural numbers
    static long fourthPowerSum(int n)
    {
        return ((6 * n * n * n * n * n) +
                (15 * n * n * n * n) +
                (10 * n * n * n) - n) / 30;
    }
     
    public static void main (String[] args)
    {
        int n = 6;
         
        System.out.println(fourthPowerSum(n));
     
    }
}
 
// This code is contributed by Gitanjali.


Python3
# Python3 Program to
# find the sum of
# forth powers of
# first n natural numbers
import math
 
# Return the sum of
# forth power of
# first n natural
# numbers
def fourthPowerSum(n):
 
    return ((6 * n * n * n * n * n) +
            (15 * n * n * n * n) +
            (10 * n * n * n) - n) / 30
     
# Driver method
n=6
print (fourthPowerSum(n))
 
# This code is contributed by Gitanjali.


C#
// C# Program to find the
// sum of forth powers of
// first n natural numbers
using System;
 
class GFG {
     
    // Return the sum of
    // forth power of first
    // n natural numbers
    static long fourthPowerSum(int n)
    {
        return ((6 * n * n * n * n * n) +
                (15 * n * n * n * n) +
                (10 * n * n * n) - n) / 30;
    }
     
    public static void Main ()
    {
        int n = 6;
         
        Console.Write(fourthPowerSum(n));
     
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

2275

时间复杂度:O(n)
有效的方法:-一个有效的解决方案是使用直接数学公式,它是1 / 30n(n + 1)(2n + 1)(3n2 + 3n + 1)或也写为(1/5)n 5 +(1 / 2)n 4 +(1/3)n 3 –(1/30)n。此解决方案需要O(1)时间。

C++

// CPP Program to find the sum of forth power of first
// n natural numbers
#include 
using namespace std;
 
// Return the sum of forth power of first n natural
// numbers
long long int fourthPowerSum(int n)
{
    return ((6 * n * n * n * n * n) +
            (15 * n * n * n * n) +
            (10 * n * n * n) - n) / 30;
}
 
// Driven Program
int main()
{
    int n = 6;
    cout << fourthPowerSum(n) << endl;
    return 0;
}

Java

// Java Program to find the
// sum of forth powers of
// first n natural numbers
import java.io.*;
import java.util.*;
 
class GFG {
     
    // Return the sum of
    // forth power of first
    // n natural numbers
    static long fourthPowerSum(int n)
    {
        return ((6 * n * n * n * n * n) +
                (15 * n * n * n * n) +
                (10 * n * n * n) - n) / 30;
    }
     
    public static void main (String[] args)
    {
        int n = 6;
         
        System.out.println(fourthPowerSum(n));
     
    }
}
 
// This code is contributed by Gitanjali.

Python3

# Python3 Program to
# find the sum of
# forth powers of
# first n natural numbers
import math
 
# Return the sum of
# forth power of
# first n natural
# numbers
def fourthPowerSum(n):
 
    return ((6 * n * n * n * n * n) +
            (15 * n * n * n * n) +
            (10 * n * n * n) - n) / 30
     
# Driver method
n=6
print (fourthPowerSum(n))
 
# This code is contributed by Gitanjali.

C#

// C# Program to find the
// sum of forth powers of
// first n natural numbers
using System;
 
class GFG {
     
    // Return the sum of
    // forth power of first
    // n natural numbers
    static long fourthPowerSum(int n)
    {
        return ((6 * n * n * n * n * n) +
                (15 * n * n * n * n) +
                (10 * n * n * n) - n) / 30;
    }
     
    public static void Main ()
    {
        int n = 6;
         
        Console.Write(fourthPowerSum(n));
     
    }
}
 
// This code is contributed by vt_m.

的PHP


Java脚本


输出:

2275

时间复杂度:O(1)