📜  前n个自然数的k次幂之和

📅  最后修改于: 2021-09-22 09:40:30             🧑  作者: Mango

给定两个整数nk ,任务是计算并打印1 k + 2 k + 3 k + … + n k
例子:

方法:前n个自然数的平方和的证明:

添加所有方程:

类似地,立方体的证明可以通过以下方式显示:

同样对于 k 次幂和,

下面是上述方法的实现:

C++
// C++ program to find sum pf k-th powers of
// first n natural numbers.
#include 
using namespace std;
 
// A global array to store factorials
const int MAX_K = 15;
long long unsigned int fac[MAX_K];
 
// Function to calculate the factorials
// of all the numbers upto k
void factorial(int k)
{
    fac[0] = 1;
    for (int i = 1; i <= k + 1; i++) {
        fac[i] = (i * fac[i - 1]);
    }
}
 
// Function to return the binomial coefficient
long long unsigned int bin(int a, int b)
{
 
    // nCr = (n! * (n - r)!) / r!
    long long unsigned int ans =
               ((fac[a]) / (fac[a - b] * fac[b]));
    return ans;
}
 
// Function to return the sum of kth powers of
// n natural numbers
long int sumofn(int n, int k)
{
    int p = 0;
    long long unsigned int num1, temp, arr[1000];
    for (int j = 1; j <= k; j++) {
 
        // When j is unity
        if (j == 1) {
            num1 = (n * (n + 1)) / 2;
 
            // Calculating sum(n^1) of unity powers
            // of n; storing sum(n^1) for sum(n^2)
            arr[p++] = num1;
 
            // If k = 1 then temp is the result
            temp = num1;
        }
        else {
            temp = (pow(n + 1, j + 1) - 1 - n);
 
            // For finding sum(n^k) removing 1 and
            // n * kCk from (n + 1)^k
            for (int s = 1; s < j; s++) {
 
                // Removing all kC2 * sum(n^(k - 2))
                // + ... + kCk - 1 * (sum(n^(k - (k - 1))
                temp = temp -
                    (arr[j - s - 1] * bin(j + 1, s + 1));
            }
            temp = temp / (j + 1);
 
            // Storing the result for next sum of
            // next powers of k
            arr[p++] = temp;
        }
    }
    temp = arr[p - 1];
    return temp;
}
 
// Driver code
int main()
{
    int n = 5, k = 2;
    factorial(k);
    cout << sumofn(n, k) << "\n";
    return 0;
}


Java
// Java program to find sum pf k-th powers of
// first n natural numbers.
 
import java.io.*;
 
class GFG {
 
 
// A global array to store factorials
static int MAX_K = 15;
static int fac[] = new int[MAX_K];
 
// Function to calculate the factorials
// of all the numbers upto k
static void factorial(int k)
{
    fac[0] = 1;
    for (int i = 1; i <= k + 1; i++) {
        fac[i] = (i * fac[i - 1]);
    }
}
 
// Function to return the binomial coefficient
static  int bin(int a, int b)
{
 
    // nCr = (n! * (n - r)!) / r!
    int ans =
            ((fac[a]) / (fac[a - b] * fac[b]));
    return ans;
}
 
// Function to return the sum of kth powers of
// n natural numbers
static int sumofn(int n, int k)
{
    int p = 0;
    int num1, temp;
    int arr[] = new int[1000];
    for (int j = 1; j <= k; j++) {
 
        // When j is unity
        if (j == 1) {
            num1 = (n * (n + 1)) / 2;
 
            // Calculating sum(n^1) of unity powers
            // of n; storing sum(n^1) for sum(n^2)
            arr[p++] = num1;
 
            // If k = 1 then temp is the result
            temp = num1;
        }
        else {
            temp = ((int)Math.pow(n + 1, j + 1) - 1 - n);
 
            // For finding sum(n^k) removing 1 and
            // n * kCk from (n + 1)^k
            for (int s = 1; s < j; s++) {
 
                // Removing all kC2 * sum(n^(k - 2))
                // + ... + kCk - 1 * (sum(n^(k - (k - 1))
                temp = temp -
                    (arr[j - s - 1] * bin(j + 1, s + 1));
            }
            temp = temp / (j + 1);
 
            // Storing the result for next sum of
            // next powers of k
            arr[p++] = temp;
        }
    }
    temp = arr[p - 1];
    return temp;
}
 
// Driver code
 
    public static void main (String[] args) {
            int n = 5, k = 2;
    factorial(k);
    System.out.println( sumofn(n, k));
    }
}
// This code is contributed by anuj_67..


Python3
# Python3 program to find the sum pf k-th
# powers of first n natural numbers
 
# global array to store factorials
MAX_K = 15
fac = [1 for i in range(MAX_K)]
 
# function to calculate the factorials
# of all the numbers upto k
def factorial(k):
    fac[0] = 1
    for i in range(1, k + 2):
        fac[i] = (i * fac[i - 1])
 
# function to return the binomial coeff
def bin(a, b):
     
    # nCr=(n!*(n-r)!)/r!
    ans = fac[a] // (fac[a - b] * fac[b])
    return ans
     
# function to return the sum of the kth
# powers of n natural numbers
def sumofn(n, k):
    p = 0
    num1, temp = 1, 1
    arr = [1 for i in range(1000)]
     
    for j in range(1, k + 1):
         
        # when j is 1
        if j == 1:
            num1 = (n * (n + 1)) // 2
             
            # calculating sum(n^1) of unity powers
            #of n storing sum(n^1) for sum(n^2)
            arr[p] = num1
            p += 1
             
            # if k==1 then temp is the result
        else:
            temp = pow(n + 1, j + 1) - 1 - n
             
            # for finding sum(n^k) removing 1 and
            # n*KCk from (n+1)^k
            for s in range(1, j):
                 
                # Removing all kC2 * sum(n^(k - 2))
                # + ... + kCk - 1 * (sum(n^(k - (k - 1))
                temp = temp - (arr[j - s - 1] *
                               bin(j + 1, s + 1))
            temp = temp // (j + 1)
             
            # storing the result for next sum
            # of next powers of k
            arr[p] = temp
            p += 1
    temp = arr[p - 1]
    return temp
 
# Driver code
n, k = 5, 2
factorial(k)
print(sumofn(n, k))
 
# This code is contributed by Mohit kumar 29


C#
// C# program to find sum pf k-th powers of
// first n natural numbers.
 
using System;
 
class GFG {
 
// A global array to store factorials
static int MAX_K = 15;
static int []fac = new int[MAX_K];
 
// Function to calculate the factorials
// of all the numbers upto k
static void factorial(int k)
{
    fac[0] = 1;
    for (int i = 1; i <= k + 1; i++) {
        fac[i] = (i * fac[i - 1]);
    }
}
 
// Function to return the binomial coefficient
static int bin(int a, int b)
{
 
    // nCr = (n! * (n - r)!) / r!
    int ans =
            ((fac[a]) / (fac[a - b] * fac[b]));
    return ans;
}
 
// Function to return the sum of kth powers of
// n natural numbers
static int sumofn(int n, int k)
{
    int p = 0;
    int num1, temp;
    int []arr = new int[1000];
    for (int j = 1; j <= k; j++) {
 
        // When j is unity
        if (j == 1) {
            num1 = (n * (n + 1)) / 2;
 
            // Calculating sum(n^1) of unity powers
            // of n; storing sum(n^1) for sum(n^2)
            arr[p++] = num1;
 
            // If k = 1 then temp is the result
            temp = num1;
        }
        else {
            temp = ((int)Math.Pow(n + 1, j + 1) - 1 - n);
 
            // For finding sum(n^k) removing 1 and
            // n * kCk from (n + 1)^k
            for (int s = 1; s < j; s++) {
 
                // Removing all kC2 * sum(n^(k - 2))
                // + ... + kCk - 1 * (sum(n^(k - (k - 1))
                temp = temp -
                    (arr[j - s - 1] * bin(j + 1, s + 1));
            }
            temp = temp / (j + 1);
 
            // Storing the result for next sum of
            // next powers of k
            arr[p++] = temp;
        }
    }
    temp = arr[p - 1];
    return temp;
}
 
    // Driver code
    public static void Main () {
            int n = 5, k = 2;
            factorial(k);
            Console.WriteLine(sumofn(n, k));
    }
    // This code is contributed by Ryuga
}


PHP


Javascript


输出:
55

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程