Home > Backend Development > C++ > body text

The sum of the first n natural numbers raised to fifth powers

PHPz
Release: 2023-09-11 14:45:09
forward
1266 people have browsed it

The sum of the first n natural numbers raised to fifth powers

Natural numbers are numbers starting from 1 and including all positive integers. The following article discusses two possible ways to calculate the sum of the fifth power of the first n natural numbers. This article discusses both approaches in detail and compares them in terms of efficiency and intuitiveness.

Problem Statement

The purpose of this problem is to calculate the arithmetic sum of the first n natural numbers, all numbers raised to their fifth power, that is

$\mathrm{1^5 2^5 3^5 4^5 5^5 … n^5}$ Until the nth item.

Example

Since n is a natural number, its value cannot be less than 1.

Input: n = 3
Copy after login
Output: 276
Copy after login

Explanation

$\mathrm{1^5 = 1 * 1 * 1 * 1 * 1 = 1}$

$\mathrm{2^5 = 2 * 2 * 2 * 2 * 2 = 32}$

$\mathrm{3^5 = 3 * 3 * 3 * 3 * 3 = 243}$

Adding these terms, we get $ \mathrm{1^5 2^5 3^5 = 276}$

Therefore, the sum of the first 3 natural numbers is 276.

Input: n = 1
Copy after login
Output: 1
Copy after login

Explanation

$\mathrm{1^5 = 1 * 1 * 1 * 1 * 1 = 1}$

So the sum of the first 1 natural number is 1.

Input: n = 11
Copy after login
Output: 381876
Copy after login

Description

$\mathrm{1^5 = 1 * 1 * 1 * 1 * 1 = 1}$

$\mathrm{2^5 = 2 * 2 * 2 * 2 * 2 = 32}$

....

$\mathrm{11^5 = 11 * 11 * 11 * 11 * 11 = 161051} $

After adding these terms, we get $\mathrm{1^5 2^5 3^5 ... 11^ 5 = 381876}$

So the sum of the first 11 natural numbers is 381876.

Intuitive method

  • Use an iterative loop to calculate the fifth power of each number one by one.

  • Create a variable to store the sum after each loop iteration.

  • Show answer.

algorithm

Function main()

  • Initialize n.

  • The function calls sumOfFifthPower().

  • Print the sum.

Function sumOfFifthPower(int n)

  • Initialize sum = 0

  • for (i from 1 to n)

    • sum = sum (pow(i,5)

  • Return the sum

Example

This program calculates the fifth power of each number and adds it to the existing sum on each iteration using a for loop implemented n times in the function

sumOfFifthPower() .

// A C++ program to find the sum of the first n natural numbers, all raised to their fifth power.
#include <iostream>
#include <cmath>
using namespace std;
// This function calculates the summation of fifth powers of the first // n natural numbers and stores
// it in the variable sum
int sumOfFifthPower(int n){
   int sum = 0;
   for (int i = 1; i <= n; i++)    {
   
      // calculate fifth power of i and add it to sum
      sum = sum + pow(i, 5);
   }
   return sum;
}

// main function
int main(){
   int n = 3;
   int ans; // to store final result
   ans = sumOfFifthPower(n); // function call
   cout << "The sum of the fifth powers of the first " << n << " natural numbers is: ";
   cout << ans; // Display the final result
   return 0;
}
Copy after login

Output

The sum of the fifth powers of the first 3 natural numbers is: 276
Copy after login
Copy after login

Space-time analysis

Time complexity: O(n), because only one for loop is used inside the function sumOfFifthPower().

Space complexity: O(1), because no additional space is used.

alternative method

  • Use mathematical formulas to calculate the sum of the fifth power of each number.

  • Show answer.

formula

$$\mathrm{\displaystyle\sum\limits_{k=1}^n \:k^5=\frac{1}{12}(2n^6 6n^5 5n^4−n^ 2) }$$

algorithm

Function main()

  • Initialize n.

  • The function calls sumOfFifthPower().

  • Print the sum.

Function sumOfFifthPower(int n)

  • Initialize sum = 0

  • Sum = ((2 * pow(n,6)) (6 * pow(n,5) (5 * pow(n,4) - (pow(n,2)) / 12

  • Return the sum

Example

This program calculates the sum by substituting the value of n into a mathematical formula that calculates the sum of the first n natural numbers raised to the fifth power in the function

sumOfFifithPower().

// A C++ program to find the sum of the first n natural numbers, all raised to their fifth power.
#include <iostream>
#include <cmath>
using namespace std;

// This function calculates the summation of fifth powers of the first // n natural numbers and stores it in the variable sum
int sumOfFifthPower(int x){
   int sum = 0;
   sum = ((2 * pow(x,6)) + (6 * pow(x,5)) + (5 *pow(x,4)) - (pow(x,2))) / 12; 
   return sum;
}

// main function
int main(){
   int n = 3;
   int ans; // to store final result
   ans = sumOfFifthPower(n); // function call
   cout << "The sum of the fifth powers of the first " << n << " natural numbers is: ";
   cout << ans; // Display the final result
   return 0;
}
Copy after login

Output

The sum of the fifth powers of the first 3 natural numbers is: 276
Copy after login
Copy after login
Space-time analysis

Time complexity: O(1), since the answer is calculated in a single iteration using the direct formula.

Space complexity: O(1), because no additional space is required.

Compare the above methods

standardmethod 1Method 2time complexityO(n)O(1)Space complexityO(1)O(1)IntuitivenessMoreLessLessefficiencyLessLessMore
The Chinese translation of is:
The Chinese translation of is:
in conclusion

This article discusses two methods to calculate the sum of the fifth powers of the first n natural numbers. It also introduces the concepts of both methods, algorithms, C program solutions, and complexity analysis of each method. It can be observed that the first method has higher time complexity but is more intuitive. The second approach, on the other hand, uses straightforward mathematical formulas to solve the problem efficiently in O(1) time and space.

The above is the detailed content of The sum of the first n natural numbers raised to fifth powers. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template