harmonic progression sum
given the first element of the progression āaā, common difference between the element ādā and number of terms in the progression ānā, where n, d, a \in [1, 1]. 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
recommended: please try your approach on {ide} first, before moving on to the solution.
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.
// java code to generate harmonic progression
// and calculate the sum of the progression.
import java.util.*;
import java.lang.*;
class geeksforgeeks {
// function that generates the harmonic progression
// and calculates the sum of its elements by iterating.
static double generateap(int a, int d, int n, int ap[])
{
double sum = 0;
for (int i = 1; i < = n; i++) {
// hp = 1/ap
// in ap, ith term is calculated by a+(i-1)d;
ap[i] = (a + (i - 1) * d);
// calculating the sum.
sum += (double)1 / ( + (i - 1) * d));
}
return sum;
}
// function that uses riemenn sum method to calculate
// the approximate sum of hp in o(1) time complexity
static double sumapproximation(int a, int d, int n)
{
return math.log((2 * a + (2 * n - 1) * d) /
(2 * a - d)) / d;
}
public static void main(string args[])
{
int a = 12, d = 12, n = 5;
int ap[] = new int[n + 5];
// generating ap from the above data
double sum = generateap(a, d, n, ap);
// generating hp from the generated ap
system.out.println("harmonic progression : ");
for (int i = 1; i < = n; i++)
system.out.print("1/" + ap[i] + " ");
system.out.println();
string str = "";
str = str + sum;
str = str.substring(0, 4);
system.out.println("sum of the generated" +
" harmonic progression : " + str);
sum = sumapproximation(a, d, n);
str = "";
str = str + sum;
str = str.substring(0, 4);
system.out.println("sum of the generated " +
"harmonic progression using approximation : "
+ str);
}
}
run on ide
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