
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program for Finding Nth Term of H.P.
What is Harmonic Progression?
A Harmonic Progression is a sequence of numbers formed by the reciprocal of an Arithmetic progression sequence.
If a given sequence a1, a2, a3, a4... is in Arithmetic progression, then 1/a1, 1/a2, 1/a3, 1/a4... forms a Harmonic Progression.
Formula for the nth term of Harmonic Progression?
The formula for the nth term of a Harmonic Progression is:
Tn = 1 / (1/a1 + (n - 1) * d)
where:
a1 is the first term of the Harmonic Progression.
d is the common difference of the corresponding Arithmetic Progression obtained by the reciprocals of the terms of the Harmonic Progression.
n is the term we want to find.
Problem Description
In this problem, we are given the first term a1, the common difference d of the corresponding Arithmetic Progression, and we have to find the nth term of the Harmonic Progression. Below are some examples to understand the problem statement clearly:
Example 1
-
Input:
a1 = 1, d = 1, n = 5 - Output: T4 = 0.25
Explanation
For the given input, the Arithmetic Progression is 1, 2, 3, 4, and 5; therefore, the Harmonic Progression will be 1, 1/2, 1/3, 1/4, and 1/5. So, the 4th term of the given input will be 1/4 = 0.25.
Example 2
-
Input:
a1 = 2, d = 2, n = 3 - Output: T3 = 0.16667
Explanation
For the given input, the Arithmetic Progression is 2, 4, and 6; therefore, the Harmonic Progression will be 1/2, 1/4, and 1/6. So, the 3rd term of the given input will be 1/6 = 0.16667.
Brute Force Approach
In this approach, we will directly calculate the nth term of H.P. by using the direct formula for the nth term of Harmonic Progression. The formula for the nth term of a Harmonic progression is
Tn = 1 / (1/a1 + (n - 1) * d)
Steps for Implementation
- Use the formula for the nth term of H.P.: Tn = 1 / (1/a1 + (n -1) * d)
- Input the values of the first term a1, common difference d, and nth term n into the formula.
- Return the output.
Implementation Code
#include <bits/stdc++.h> using namespace std; // Function to calculate the nth term of Harmonic Progression double NthTermOfHarmonic(double a1, double d, int n) { // Calculate the reciprocal double reciprocal = 1 / a1 + (n - 1) * d; // Return the nth term return 1 / reciprocal; } int main() { // First term and common difference double a1 = 1, d = 1; // nth term we want to find int n = 5; // Output the nth term of the H.P. cout << "The " << n << "th term of Harmonic Progression is: " << NthTermOfHarmonic(a1, d, n) << endl; return 0; }
Output
The 5th term of Harmonic Progression is: 0.2
Time Complexity: O(1)
Space Complexity: O(1)
Using Iterative Approach
In this iterative approach, we iteratively calculate the reciprocal of each term in the Harmonic Progression, using the formula for the nth term of the corresponding Arithmetic Progression (A.P.).
Steps for Implementation
- Take the first term a1.
- Now, iterate n-1 times to calculate the nth term using the formula for the Arithmetic Progression.
- Return the result value of the nth term, i.e., 1/n
Implementation Code
#include <bits/stdc++.h> using namespace std; // Function to find the nth term double findNthTermHarmonicIterative(double a1, double d, int n) { double reciprocal = 1 / a1; // Iterate from 1 to n-1 for (int i = 1; i < n; i++) { reciprocal += d; } return 1 / reciprocal; // Return the reciprocal of the calculated value for nth term } int main() { double a1 = 1, d = 1; // first term (a1) and the common difference (d) int n = 5; // nth term we want to find // Output cout << "The " << n << "th term of Harmonic Progression is: " << findNthTermHarmonicIterative(a1, d, n) << endl; return 0; // End of the program }
Output
The 5th term of Harmonic Progression is: 0.2
Time Complexity: O(n)
Space Complexity: O(1)