Python Program for finding nth term of H.P.

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.

Formula for 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 nth 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
  • Find the reciprocal of the result to get the nth term of the H.P.

Input & Output Scenarios

Example 1:

Input:
First term = 2
Common difference of A.P. = 3
n = 4

Output: 2/19

Explanation:

  • 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 2:

Input:
First term = 5
Common difference of A.P. = 2
n = 6

Output: 5/51

Explanation:

  • Convert H.P. into A.P.
  • 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. = 5/51

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 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}")
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 ?

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}")
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 ?

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}")
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 ?

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}")
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 ?

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}")
The 4th term of the Harmonic Progression is: 0.10526315789473684

Time Complexity: O(n)

Conclusion

We explored multiple approaches for finding the nth term of a Harmonic Progression using Python. The formula approach is most efficient with O(1) time complexity, while the iterative approach provides step-by-step calculations. Choose the method that best fits your coding style and requirements.

Updated on: 2026-03-27T16:53:07+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements