![]() |
VOOZH | about |
Given the first element of the progression 'a', common difference between the element 'd' and number of terms in the progression 'n', where . The task is to generate harmonic progression using the above set of information.
Examples:
Input : a = 12, d = 12, n = 5 Output : Harmonic Progression : 1/12 1/24 1/36 1/48 1/60 Sum of the generated harmonic progression : 0.19 Sum of the generated harmonic progression using approximation :0.19
Arithmetic Progression : In an arithmetic progression (AP) or arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant.
Harmonic Progression: A harmonic progression (or harmonic sequence) is a progression formed by taking the reciprocals of an arithmetic progression.
Now, we need to generate this harmonic progression. We even have to calculate the sum of the generated sequence.
1. Generating of HP or 1/AP is a simple task. The Nth term in an AP = a + (n-1)d. Using this formula, we can easily generate the sequence.
2. Calculating the sum of this progression or sequence can be a time taking task. We can either iterate while generating this sequence or we could use some approximations and come up with a formula which would give us a value accurate up to some decimal places. Below is an approximate formula.
Sum = 1/d (ln(2a + (2n - 1)d) / (2a - d))
Please refer brilliant.org for details of above formula.
Below is implementation of above formula.
Output:
Harmonic Progression : 1/12 1/24 1/36 1/48 1/60 Sum of the generated harmonic progression : 0.19 Sum of the generated harmonic progression using approximation :0.19
Time Complexity: O(n)
Auxiliary Space: O(n)