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
. 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.
Java
// 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 / (double)((a + (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); } } |
Python3
# Python3 code to generate Harmonic Progression # and calculate the sum of the progression. import math # Function that generates the harmonic # progression and calculates the sum of # its elements by iterating. n = 5; AP = [0] * (n + 5); def generateAP(a, d, n): sum = 0; for i in range(1, n + 1): # HP = 1/AP # In AP, ith term is calculated # by a+(i-1)d; AP[i] = (a + (i - 1) * d); # Calculating the sum. sum += float(1) / float((a + (i - 1) * d)); return sum; # Function that uses riemenn sum method to calculate # the approximate sum of HP in O(1) time complexity def sumApproximation(a, d, n): return math.log((2 * a + (2 * n - 1) * d) / (2 * a - d)) / d; # Driver Code a = 12; d = 12; #n = 5; # Generating AP from the above data sum = generateAP(a, d, n); # Generating HP from the generated AP print("Harmonic Progression :"); for i in range(1, n + 1): print("1 /", AP[i], end = " "); print(""); str1 = ""; str1 = str1 + str(sum); str1 = str1[0:4]; print("Sum of the generated harmonic", "progression :", str1); sum = sumApproximation(a, d, n); str1 = ""; str1 = str1 + str(sum); str1 = str1[0:4]; print("Sum of the generated harmonic", "progression using approximation :", str1); # This code is contributed by mits |
C#
// C# code to generate Harmonic // Progression and calculate // the sum of the progression. using System; class GFG { // 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 / (double)((a + (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; } // Driver code static void Main() { 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 Console.WriteLine("Harmonic Progression :"); for (int i = 1; i <= n; i++) Console.Write("1/" + AP[i] + " "); Console.WriteLine(); String str = ""; str = str + sum; str = str.Substring(0, 4); Console.WriteLine("Sum of the generated" + " harmonic progression : " + str); sum = sumApproximation(a, d, n); str = ""; str = str + sum; str = str.Substring(0, 4); Console.WriteLine("Sum of the generated " + "harmonic progression using approximation : " + str); } } // This code is contributed by // ManishShaw(manishshaw1) |
PHP
<?php // PHP code to generate Harmonic Progression // and calculate the sum of the progression. // Function that generates the harmonic progression // and calculates the sum of its elements by iterating. function generateAP($a, $d, $n, &$AP) { $sum = 0; for ($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 / (double)(($a + ($i - 1) * $d)); } return $sum; } // Function that uses riemenn sum method to calculate // the approximate sum of HP in O(1) time complexity function sumApproximation($a, $d, $n) { return log((2 * $a + (2 * $n - 1) * $d) / (2 * $a - $d)) / $d; } // Drive main $a = 12; $d = 12; $n = 5; $AP = array_fill(0,$n + 5,0); // Generating AP from the above data $sum = generateAP($a, $d, $n, $AP); // Generating HP from the generated AP echo "Harmonic Progression :\n"; for ($i = 1; $i <= $n; $i++) echo "1/".$AP[$i]." "; echo "\n"; $str = ""; $str = $str.strval($sum); $str = substr($str,0, 4); echo "Sum of the generated". " harmonic progression : ".$str; $sum = sumApproximation($a, $d, $n); $str = ""; $str = $str + strval($sum); $str = substr($str,0, 4); echo "\nSum of the generated ". "harmonic progression using approximation : ".$str; // this code is contributed by mits ?> |
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 Posts:
- Harmonic Progression
- Summation of floor of harmonic progression
- Leibniz harmonic triangle
- Program for harmonic mean of numbers
- Program to find the Nth Harmonic Number
- Find Harmonic mean using Arithmetic mean and Geometric mean
- Program to find sum of harmonic series
- Geometric Progression
- Arithmetic Progression
- Program to print GP (Geometric Progression)
- Ratio of mth and nth term in an Arithmetic Progression (AP)
- Program to print Arithmetic Progression series
- Program for N-th term of Arithmetic Progression series
- Number of GP (Geometric Progression) subsequences of size 3
- Program for N-th term of Geometric Progression series
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


