📜  前n个自然数的第五次幂的和

📅  最后修改于: 2021-05-06 22:39:56             🧑  作者: Mango

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

Input  : 4
Output : 1300
15 + 25 + 35 + 45 = 1300 

Input  : 6
Output : 
15 + 25 + 35 + 45 + 52 + 65

天真的方法:-在此简单的查找中,前n个自然数的第五次幂是从1到n时间的循环迭代。例如假设n = 5。并存储在sum变量中。
(1 * 1 * 1 * 1 * 1)+(2 * 2 * 2 * 2 * 2)+(3 * 3 * 3 * 3 * 3)+(4 * 4 * 4 * 4 * 4)= 1300

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


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


Python3
# Python 3 Program to find the
# sum of fifth powers of first
# n natural numbers
 
# calculate the sum of fifth
# power of first n natural
# numbers
def fifthPowerSum(n) :
 
    sm = 0
     
    for i in range(1, n+1) :
        sm = sm + (i * i * i * i * i)
     
    return sm
 
# Driven Program
n = 6
print(fifthPowerSum(n))
 
# This code is contributed
# by Nikita Tiwari.


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


PHP


Javascript


C++
// CPP Program to find the sum of fifth power
// of first n natural numbers
#include 
using namespace std;
 
// calculate the sum of fifth power of first n natural numbers
long long int fifthPowerSum(int n)
{
    return ((2 * n * n * n * n * n * n) +
           (6 * n * n * n * n * n) +
           (5 * n * n * n * n) -
           (n * n)) / 12;
}
 
// Driven Program
int main()
{
    int n = 5;
    cout << fifthPowerSum(n) << endl;
    return 0;
}


Java
// Java Program to find the sum of fifth power
// of first n natural numbers
import java.io.*;
 
class GFG {
     
    // calculate the sum of fifth power
    //of first n natural numbers
    static long fifthPowerSum(int n)
    {
        return ((2 * n * n * n * n * n * n) +
            (6 * n * n * n * n * n) +
            (5 * n * n * n * n) -
            (n * n)) / 12;
    }
     
    // Driven Program
    public static void main(String args[])
    {
        int n = 5;
        System.out.println(fifthPowerSum(n));
    }
}
 
 /*This code is contributed by Nikita Tiwari.*/


Python3
# Python 3 Program to find the
# sum of fifth power of first
# n natural numbers
 
# Calculate the sum of fifth
# power of first n natural
# numbers
def fifthPowerSum(n) :
    return ((2 * n * n * n * n * n * n) +
            (6 * n * n * n * n * n) +
            (5 * n * n * n * n) -
            (n * n)) // 12
 
 
# Driven Program
n = 5
print(fifthPowerSum(n))
 
# This code is contributed by Nikita Tiwari.


C#
// C# Program to find the sum
// of fifth power of first n
// natural numbers
using System;
 
class GFG {
     
    // calculate the sum of fifth power
    // of first n natural numbers
    static long fifthPowerSum(int n)
    {
        return ((2 * n * n * n * n * n * n) +
            (6 * n * n * n * n * n) +
            (5 * n * n * n * n) -
            (n * n)) / 12;
    }
     
    // Driven Program
    public static void Main()
    {
        int n = 5;
        Console.Write(fifthPowerSum(n));
    }
}
 
/*This code is contributed by vt_m.*/


PHP


Javascript


输出:

12201

时间复杂度: O(N)
有效的方法:-一个有效的解决方案是使用直接的数学公式,它是:

(2*n6+6*n5+5*n4 - n2)/12  

OR (Can also be written as)

(1/6)n6 + (1/2)n5 + (5/12)n4 – (1/12)n2. 

C++

// CPP Program to find the sum of fifth power
// of first n natural numbers
#include 
using namespace std;
 
// calculate the sum of fifth power of first n natural numbers
long long int fifthPowerSum(int n)
{
    return ((2 * n * n * n * n * n * n) +
           (6 * n * n * n * n * n) +
           (5 * n * n * n * n) -
           (n * n)) / 12;
}
 
// Driven Program
int main()
{
    int n = 5;
    cout << fifthPowerSum(n) << endl;
    return 0;
}

Java

// Java Program to find the sum of fifth power
// of first n natural numbers
import java.io.*;
 
class GFG {
     
    // calculate the sum of fifth power
    //of first n natural numbers
    static long fifthPowerSum(int n)
    {
        return ((2 * n * n * n * n * n * n) +
            (6 * n * n * n * n * n) +
            (5 * n * n * n * n) -
            (n * n)) / 12;
    }
     
    // Driven Program
    public static void main(String args[])
    {
        int n = 5;
        System.out.println(fifthPowerSum(n));
    }
}
 
 /*This code is contributed by Nikita Tiwari.*/

Python3

# Python 3 Program to find the
# sum of fifth power of first
# n natural numbers
 
# Calculate the sum of fifth
# power of first n natural
# numbers
def fifthPowerSum(n) :
    return ((2 * n * n * n * n * n * n) +
            (6 * n * n * n * n * n) +
            (5 * n * n * n * n) -
            (n * n)) // 12
 
 
# Driven Program
n = 5
print(fifthPowerSum(n))
 
# This code is contributed by Nikita Tiwari.

C#

// C# Program to find the sum
// of fifth power of first n
// natural numbers
using System;
 
class GFG {
     
    // calculate the sum of fifth power
    // of first n natural numbers
    static long fifthPowerSum(int n)
    {
        return ((2 * n * n * n * n * n * n) +
            (6 * n * n * n * n * n) +
            (5 * n * n * n * n) -
            (n * n)) / 12;
    }
     
    // Driven Program
    public static void Main()
    {
        int n = 5;
        Console.Write(fifthPowerSum(n));
    }
}
 
/*This code is contributed by vt_m.*/

的PHP


Java脚本


输出:

4425

时间复杂度:O(1)