📜  前n个偶数的四次方和

📅  最后修改于: 2021-04-29 03:22:50             🧑  作者: Mango

编写程序以查找前n个偶数自然数的四次幂之和。
2 4 + 4 4 + 6 4 + 8 4 + 10 4 +………+ 2n 4
例子:

Input  :   3
Output :  1568
24 +44 +64 = 1568
Input  :   6
Output :  36400
24 + 44 + 64 + 84 + 104 + 124 

天真的方法:-在这个简单的函数中,找到前n个偶数个自然数的第4次幂是从1到n次循环。第i次迭代都存储在变量中,并一直持续到(i!= n)。这需要O(N)时间复杂度。

C++
// CPP Program to find the sum of fourth powers of
// first n even natural numbers
#include 
using namespace std;
 
// calculate the sum of fourth power of first n even
// natural numbers
long long int evenPowerSum(int n)
{
    long long int sum = 0;
    for (int i = 1; i <= n; i++) {
 
        // made even number
        int j = 2 * i;
        sum = sum + (j * j * j * j);
    }
    return sum;
}
 
// Driven Program
int main()
{
    int n = 5;
    cout << evenPowerSum(n) << endl;
    return 0;
}


Java
// Java Program to find the sum of fourth powers of
// first n even natural numbers
 
import java.io.*;
 
class GFG {
     
    // calculate the sum of fourth power of first
    // n even natural numbers
    static long evenPowerSum(int n)
    {
        long sum = 0;
        for (int i = 1; i <= n; i++)
        {
     
            // made even number
            int j = 2 * i;
            sum = sum + (j * j * j * j);
        }
         
        return sum;
    }
 
    // Driven Program
    public static void main (String[] args) {
         
        int n = 5;
        System.out.println(evenPowerSum(n));
    }
}
 
/*This code is contributed by vt_m.*/


Python3
# Python3 Program to find
# the sum of fourth powers of
# first n even natural numbers
 
# calculate the sum of fourth
# power of first n even
# natural numbers
def evenPowerSum(n):
    sum = 0;
    for i in range(1, n + 1):
         
        # made even number
        j = 2 * i;
        sum = sum + (j * j * j * j);
    return sum;
 
# Driver Code
n = 5;
print(evenPowerSum(n));
 
# This is contributed by mits.


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


PHP


Javascript


C++
// CPP Program to find the sum of fourth powers of
// first n even natural numbers
#include 
using namespace std;
 
// calculate the sum of fourth power of first n
// even natural numbers
long long int evenPowerSum(int n)
{
    return (8 * n * (n + 1) * (2 * n + 1) *
          (3 * n * n + 3 * n - 1)) / 15;
}
 
// Driven Program
int main()
{
    int n = 4;
    cout << evenPowerSum(n) << endl;
    return 0;
}


Java
// JAVA Program to find the sum of fourth powers of
// first n even natural numbers
 
import java.io.*;
 
class GFG {
         
    // calculate the sum of fourth power of first n
    // even natural numbers
    static long evenPowerSum(int n)
    {
        return (8 * n * (n + 1) * (2 * n + 1) *
                   (3 * n * n + 3 * n - 1)) / 15;
    }
     
    // Driven Program
    public static void main (String[] args) {
         
        int n = 4;
        System.out.println(evenPowerSum(n));
    }
}
 
/* This code is contributed by vt_m. */


Python3
# Python3 Program to find
# the sum of fourth powers
# of first n even natural
# numbers
 
# calculate the sum of
# fourth power of first n
# even natural numbers
def evenPowerSum(n):
    return (8 * n * (n + 1) *
           (2 * n + 1) * (3 *
            n * n + 3 * n - 1)) / 15;
 
# Driver Code
n = 4;
print (int(evenPowerSum(n)));
 
# This code is contributed by mits


C#
// C# Program to find the sum of fourth powers of
// first n even natural numbers
 
using System;
 
class GFG {
 
    // calculate the sum of fourth power of first n
    // even natural numbers
    static long evenPowerSum(int n)
    {
        return (8 * n * (n + 1) * (2 * n + 1) *
                     (3 * n * n + 3 * n - 1)) / 15;
    }
 
    // Driven Program
    public static void Main()
    {
        int n = 4;
         
        Console.Write(evenPowerSum(n));
    }
}
 
/* This code is contributed by vt_m.*/


PHP


Javascript


输出:

15664

时间复杂度:O(n)
高效的方法:-高效的解决方案是使用下面得出的直接数学公式,这仅需要O(1)时间复杂度。

C++

// CPP Program to find the sum of fourth powers of
// first n even natural numbers
#include 
using namespace std;
 
// calculate the sum of fourth power of first n
// even natural numbers
long long int evenPowerSum(int n)
{
    return (8 * n * (n + 1) * (2 * n + 1) *
          (3 * n * n + 3 * n - 1)) / 15;
}
 
// Driven Program
int main()
{
    int n = 4;
    cout << evenPowerSum(n) << endl;
    return 0;
}

Java

// JAVA Program to find the sum of fourth powers of
// first n even natural numbers
 
import java.io.*;
 
class GFG {
         
    // calculate the sum of fourth power of first n
    // even natural numbers
    static long evenPowerSum(int n)
    {
        return (8 * n * (n + 1) * (2 * n + 1) *
                   (3 * n * n + 3 * n - 1)) / 15;
    }
     
    // Driven Program
    public static void main (String[] args) {
         
        int n = 4;
        System.out.println(evenPowerSum(n));
    }
}
 
/* This code is contributed by vt_m. */

Python3

# Python3 Program to find
# the sum of fourth powers
# of first n even natural
# numbers
 
# calculate the sum of
# fourth power of first n
# even natural numbers
def evenPowerSum(n):
    return (8 * n * (n + 1) *
           (2 * n + 1) * (3 *
            n * n + 3 * n - 1)) / 15;
 
# Driver Code
n = 4;
print (int(evenPowerSum(n)));
 
# This code is contributed by mits

C#

// C# Program to find the sum of fourth powers of
// first n even natural numbers
 
using System;
 
class GFG {
 
    // calculate the sum of fourth power of first n
    // even natural numbers
    static long evenPowerSum(int n)
    {
        return (8 * n * (n + 1) * (2 * n + 1) *
                     (3 * n * n + 3 * n - 1)) / 15;
    }
 
    // Driven Program
    public static void Main()
    {
        int n = 4;
         
        Console.Write(evenPowerSum(n));
    }
}
 
/* This code is contributed by vt_m.*/

的PHP


Java脚本


输出:

5664

时间复杂度:O(1)