给定两个整数n和k ,任务是计算并打印1 k + 2 k + 3 k + … + n k 。
例子:
Input: n = 5, k = 2
Output: 55
12 + 22 + 32 + 42 + 52 = 1 + 4 + 9 + 16 + 25 = 55
Input: n = 10, k = 4
Output: 25333
方法:前n个自然数的平方和的证明:
(n+1)3 – n3 = 3 * (n2) + 3 * n + 1
putting n = 1, 2, 3, 4, …, n
23 – 13 = 3 * (12) + 3 * 1 + 1 …equation 1
33 – 23 = 3 * (22) + 3 * 2 + 1 …equation 2
43 – 33 = 3 * (32) + 3 * 3 + 1 …equation 3
……
……
……
(n + 1)3 – n3 = 3 * (n2) + 3 * n + 1 …equation n
添加所有方程:
(n + 1)3 – 13 = 3 * (sum of square terms) + 3 * (sum of n terms) + n
n3 + 3 * n2 + 3 * n = 3 * (sum of square terms) + (3 * n * (n + 1)) / 2 + n
sum of square terms = (n * (n + 1) * (2 * n + 1)) / 6
类似地,立方体的证明可以通过以下方式显示:
(n+1)4 – n4 = 4 * n3 + 6 * n2 + 4 * n + 1
if we continue this process for n5, n6, n7 … nk
Sum of squares,
(n + 1)3 – 13 = 3C1 * sum(n2) + 3C2 * sum(n) + 3C3
Using sum of squares we can find the sum of cubes,
(n + 1)4 – 14 = 4C1 * sum(n3) + 4C2 * sum(n2) + 4C3 * sum(n) + 4C4
同样对于 k 次幂和,
(n + 1)k – 1k = kC1 * sum(n(k – 1)) + kC2 * sum(n(k – 2)) + … + kC(k – 1) * (sum(n^(k-(k-1)) + kCk * n
where C stands for binomial coefficients
Use modulus function for higher values of n
下面是上述方法的实现:
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 现场工作专业课程和学生竞争性编程现场课程。