
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
Find Nth Term of Harmonic Progression in Python
A Harmonic Progression ( H.P.) is a sequence of numbers where the reciprocals of the terms form an Arithmetic Progression (A.P.). In simple terms, if we take the reciprocal of each term in an H.P., the resulting sequence will be in A.P. In this problem, we are given the first term, common difference, and value of n of which we have to find the nth term of given H.P.
Find the nth term of H.P.
To find the nth term of Harmonic Progression, we first convert the H.P. into an A.P. by finding reciprocals of the terms. The formula for finding the n-th term of an A.P. is given as:
An = A + (n ? 1) ? d
Where:
- A is the first term of the A.P. (i.e., reciprocal of the first term of H.P.).
- d is the common difference of the A.P.
- n is the term number.
- Now, find the reciprocal of the result to get the nth term of the H.P.
Input & Output Scenarios
Example
Input: First term = 2, Common difference of A.P. = 3, n = 4 Output: 2/19Explanation
- Convert H.P. into A.P.
- The first term of A.P. = 1/2.
- Common difference = 3.
- Fourth term of A.P. = 1/2+(4?1)Ã3=1/2+9=19/2.
- Taking reciprocal: nth term of H.P. = 2/19.
Example
Input: First term = 5, Common difference of A.P. = 2, n = 6 Output: 5/51Explanation
- Convert H.P. into A.P.:li>
- The first term of A.P. = 1/5.
- Common difference = 2.
- Sixth term of A.P. = 1/5 + (6?1) Ã 2 = 1/5 + 10 = 51/5.
- Taking reciprocal: nth term of H.P. = 2/19.
Using the Formula Approach
This is the most direct approach for finding the nth term of H.P. In a Harmonic Progression, the reciprocals of the terms form an Arithmetic Progression. If the first term of H.P. is A then the first term of A.P. is: 1/A.
The formula for finding the nth term of an A.P. is given as: Tn = a + (n - 1) d. The nth term of the Harmonic progression is reciprocal of the nth term of Arithmetic Progression.
Hn = 1/ Tn
Example
a = 2 d = 3 n = 4 ap_n = (1 / a) + (n - 1) * d hp_n = 1 / ap_n print(f"The {n}th term of the Harmonic Progression is: {hp_n}")
Output
The 4th term of the Harmonic Progression is: 0.10526315789473684
Time Complexity : O(1)
Using the Exponentiation Operator
Python allows exponentiation using **. Since division is the inverse of multiplication, we can calculate the H.P. term using the exponentiation operator.
Example
a = 2 d = 3 n = 4 ap_n = (1 / a) + (n - 1) * d hp_n = ap_n ** -1 print(f"The {n}th term of the Harmonic Progression is: {hp_n}")
Output
The 4th term of the Harmonic Progression is: 0.10526315789473684
Time Complexity : O(1)
Using the math.pow() Function
The math.pow() function from Python's math module can also be used to calculate the power of a number, including reciprocal calculations.
Example
import math a = 2 d = 3 n = 4 ap_n = (1 / a) + (n - 1) * d hp_n = math.pow(ap_n, -1) print(f"The {n}th term of the Harmonic Progression is: {hp_n}")
Output
The 4th term of the Harmonic Progression is: 0.10526315789473684
Time Complexity : O(1)
Using a Function
A function-based approach helps make the code reusable and allows us to find the nth term of H.P. for different values easily.
Example
def nth_term_hp(a, d, n): ap_n = (1 / a) + (n - 1) * d return 1 / ap_n a = 2 d = 3 n = 4 hp_n = nth_term_hp(a, d, n) print(f"The {n}th term of the Harmonic Progression is: {hp_n}")
Output
The 4th term of the Harmonic Progression is: 0.10526315789473684
Time Complexity : O(1)
Using a Loop (Iterative Approach)
In this approach, we use a loop to iteratively find the nth term of A.P. and then take its reciprocal to get the H.P. term.
Example
a = 2 d = 3 n = 4 ap_n = 1 / a for i in range(1, n): ap_n += d hp_n = 1 / ap_n print(f"The {n}th term of the Harmonic Progression is: {hp_n}")
Output
The 4th term of the Harmonic Progression is: 0.10526315789473684
Time Complexity : O(n)
Conclusion
In this article, we have discussed different approaches for finding the nth term of a Harmonic Progression (H.P.) using Python. We have seen:
- Formula Approach ? We have directly used the reciprocal of an A.P. term.
- Exponentiation Operator Approach ? We have used ** for reciprocal calculations.
- Using math.pow() Function ? We have used Python's built-in power function.
- Function-Based Approach ? We have defined a reusable function for calculating H.P. terms.
- Loop-Based Approach ? This is an iterative method for step-by-step calculations.