JavaScript Program for Sum of n Terms of Harmonic Progression
Last Updated :
21 Mar, 2024
One can find the sum of n terms of Harmonic Progression using JavaScript. Harmonic Progression is a sequence of real numbers in which each term is reciprocal of Arithmetic Progression.
There are different approaches to finding the sum of n terms of Harmonic Progression which are discussed below:
Using Iteration
In this approach, we will create a function and initialize a variable sum to store the sum of Harmonic Progression. We will use a loop to iterate from 1 to n and in each iteration we will add the reciprocal of the current sum i.e. (we will add 1/i) to the sum. After the loop finishes , we will return the result stored in sum variable.
Example: To demonstrate finding sum of first n terms in the H.P. series using iterative function which uses loop to traverse every element until number of terms reaches to print the result.
JavaScript
function SumOfNTermsHarmonic(n) {
let sum = 0;
for (let i = 1; i <= n; i++) {
sum += 1 / i;
}
return sum;
}
const n = 7;
const sumOfNTerms = SumOfNTermsHarmonic(n);
console.log(
`The sum of the first ${n} terms of the H.P. is: ${sumOfNTerms}`);
OutputThe sum of the first 7 terms of the H.P. is: 2.5928571428571425
Time Complexity : O(n) , we are using loop
Space Complexity : O(1) , constant space
Using Recursion
In this approach we will define a recursive function. This function stops( i.e. base case) when n is 1 , it will return 1. If n is greater than 1, recursively call the function to calculate the sum of the current term (1/n) and the sum of the previous terms (recursive call with n - 1). Return the result after recursive call stops.
Example: To demonstrate finding sum of first n terms in the G.P. series using recursive function which uses recursive calls till base case reaches to print the result.
JavaScript
function SumOfNTermsHarmonic(n) {
if (n === 1) {
return 1;
} else {
return 1 / n + SumOfNTermsHarmonic(n - 1);
}
}
const n = 7;
const sumOfNTerms = SumOfNTermsHarmonic(n);
console.log(
`The sum of the first ${n} terms of the H.P. is:${sumOfNTerms}`);
OutputThe sum of the first 7 terms of the H.P. is:2.5928571428571425
Time Complexity : O(n) , as function make recursive call n times.
Space Complexity : O(n) , n recursive calls are made.
The Sum of first n terms in H.P. is calculated by using the formula for sum of first n terms of H.P. described below :
Syntax:
Sn = (n/a) + ((n * (n - 1) / 2) * (1 / d))
where:
- Sn: is sum of first n terms of H.P.
- n: is number of terms
- a: is first term of H.P.
- d: is common difference of H.P.
Example : To demonstrate finding sum of first n terms in the H.P. series using the function which uses sum of first n terms formula to calculate sum of first n terms of an H.P. series to print the result.
JavaScript
function sumOfFirstNTermsHP(n, a, d) {
return (n / a) + ((n * (n - 1) / 2) * (1 / d));
}
const n = 7;
const firstTerm = 3;
const commonDifference = 2;
const sum =
sumOfFirstNTermsHP(n, firstTerm, commonDifference);
console.log(
"Sum of first", n, "terms of harmonic progression:", sum);
OutputSum of first 7 terms of harmonic progression: 12.833333333333334
Time Complexity : O(1) , constant time
Space Complexity : O(1) , constant space
Similar Reads
JavaScript Program for Sum of n terms of Geometric Progression
Geometric Progression is a sequence of numbers whose next term is calculated by multiplying the current term with a fixed number known as a common ratio. We are going to learn how we can find the sum of n in terms of Geometric Progression in different ways using JavaScript including iteration, recur
3 min read
JavaScript Program for Sum of n Terms of Arithmetic Progression
Arithmetic progression is a sequence of numbers in which the difference between any two consecutive terms is always equal. This difference between any two consecutive terms is known as a common difference and it can be positive, negative, or zero. Below are the approaches to find Sum of n terms in A
4 min read
JavaScript Program for Sum of Infinite Terms in Geometric Progression
Geometric Progression is a sequence of numbers whose next term is calculated by multiplying the current term with a fixed number known as the common ratio. Below are the approaches to find the sum of infinite terms in a Geometric progression using JavaScript: Table of Content Using Iterative Approac
3 min read
JavaScript Program to Find the Sum of Natural Numbers using Recursion
Finding the sum of natural numbers using recursion involves defining a function that recursively adds numbers from 1 to the given limit. The function repeatedly calls itself with decreasing values until reaching the base case, where it returns the sum. Example: Input: 5Output: 15Explanation: 1 + 2 +
1 min read
How to Find Missing Numbers in Harmonic Progression
To find missing numbers in a Harmonic Progression (HP), convert the terms to their reciprocals, forming an Arithmetic Progression (AP). Identify the common difference in the AP, then use it to calculate the missing terms in the AP. Finally, take the reciprocal of these calculated terms to get the mi
4 min read
Javascript Program To Find The Sum Of Last N Nodes Of The Given Linked List
Given a linked list and a number n. Find the sum of the last n nodes of the linked list.Constraints: 0 <= n <= number of nodes in the linked list.Examples: Input : 10->6->8->4->12, n = 2 Output : 16 Sum of last two nodes: 12 + 4 = 16 Input : 15->7->9->5->16->14, n =
10 min read
Javascript Program for Largest Sum Contiguous Subarray
Write an efficient program to find the sum of contiguous subarray within a one-dimensional array of numbers that has the largest sum. Kadane's Algorithm:Initialize: max_so_far = INT_MIN max_ending_here = 0Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_f
5 min read
Harmonic Progression
Harmonic Progression (HP) or Harmonic Sequence is defined as a sequence of real numbers obtained by taking the reciprocals of an Arithmetic Progression that excludes 0. In this progression, each term is calculated as the Harmonic Mean of its two adjacent terms.If the Arithmetic Progression is repres
7 min read
Find the Sum of the Series: 1 + 1/3 + 1/5 + ... + 1/(2n-1) using JavaScript ?
We are going to discuss how we can find the sum of series: 1 + 1/3 + 1/5 + ... ...till n terms. we will discuss some approaches with examples.Below are approaches to calculate the sum of the series:Table of ContentUsing for loop Using while loop Using RecursionUsing for loop In this approach, we are
3 min read
How to Find the Sum of an Arithmetic Sequence
Sum of Arithmetic Sequence can be calculated using the formula Sn = n/2 [2a+(nâ1)d], where a is the first term of sequence and d is common difference.Before learning how to find the sum of an arithmetic sequence, let's learn about arithmetic sequences first.What is Arithmetic Sequence?An arithmetic
4 min read