Program to find the Nth Harmonic Number
Given a number N. The task is to find the Nth Harmonic Number.
Let the nth harmonic number be Hn.
The harmonic series is as follows:
H1 = 1
H2 = H1 + 1/2
H3 = H2 + 1/3
H4 = H3 + 1/4
.
.
.
Hn = Hn-1 + 1/n
Examples:
Input : N = 5
Output : 2.45
Input : N = 9
Output : 2.71786
The idea is to traverse from H1 and then consecutively keep finding H2 from H1, H3 from H2 ….. and so on.
Below is the program to find N-th Harmonic Number:
C++
#include <iostream>
using namespace std;
double nthHarmonic( int N)
{
float harmonic = 1.00;
for ( int i = 2; i <= N; i++) {
harmonic += ( float )1 / i;
}
return harmonic;
}
int main()
{
int N = 8;
cout<<nthHarmonic(N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static double nthHarmonic( int N)
{
float harmonic = 1 ;
for ( int i = 2 ; i <= N; i++) {
harmonic += ( float ) 1 / i;
}
return harmonic;
}
public static void main (String[] args) {
int N = 8 ;
System.out.print(nthHarmonic(N));
}
}
|
Python 3
def nthHarmonic(N) :
harmonic = 1.00
for i in range ( 2 , N + 1 ) :
harmonic + = 1 / i
return harmonic
if __name__ = = "__main__" :
N = 8
print ( round (nthHarmonic(N), 5 ))
|
C#
using System;
class GFG
{
static double nthHarmonic( int N)
{
float harmonic = 1;
for ( int i = 2; i <= N; i++)
{
harmonic += ( float )1 / i;
}
return harmonic;
}
static public void Main ()
{
int N = 8;
Console.Write(nthHarmonic(N));
}
}
|
PHP
<?php
function nthHarmonic( $N )
{
$harmonic = 1.00;
for ( $i = 2; $i <= $N ; $i ++)
{
$harmonic += (float)1 / $i ;
}
return $harmonic ;
}
$N = 8;
echo nthHarmonic( $N );
?>
|
Javascript
<script>
function nthHarmonic(N)
{
let harmonic = 1.00;
for (let i = 2; i <= N; i++)
{
harmonic += parseFloat(1) / i;
}
return harmonic;
}
let N = 8;
document.write( nthHarmonic(N).toFixed(5));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1) as using constant space, since no extra space has been taken.
Approach 2: Dynamic Programming:
The DP approach is better than the simple iterative approach because it avoids recomputing the sum from scratch every time. In the simple iterative approach, we add each term of the harmonic series from 1 to N one by one in every iteration. This means that we perform N-1 additions in total, which can be time-consuming for large values of N.
we only need to perform N-1 additions once and store the results in the harmonic vector. Then, we can simply access the N-th Harmonic number from the vector in constant time, which is much faster than recomputing the sum from scratch every time.
C++
#include <iostream>
#include <vector>
using namespace std;
double nthHarmonic( int N)
{
vector< double > harmonic(N+1);
harmonic[1] = 1.0;
for ( int i = 2; i <= N; i++) {
harmonic[i] = harmonic[i-1] + ( double )1 / i;
}
return harmonic[N];
}
int main()
{
int N = 8;
cout<<nthHarmonic(N);
return 0;
}
|
Java
import java.util.*;
public class HarmonicNumber {
public static double nthHarmonic( int N) {
List<Double> harmonic = new ArrayList<>();
harmonic.add( 0.0 );
harmonic.add( 1.0 );
for ( int i = 2 ; i <= N; i++) {
harmonic.add(harmonic.get(i- 1 ) + 1.0 / i);
}
return harmonic.get(N);
}
public static void main(String[] args) {
int N = 8 ;
System.out.println(nthHarmonic(N));
}
}
|
Python3
def nthHarmonic(N):
harmonic = [ 0.0 ] * (N + 1 )
harmonic[ 1 ] = 1.0
for i in range ( 2 , N + 1 ):
harmonic[i] = harmonic[i - 1 ] + 1.0 / i
return harmonic[N]
N = 8
print (nthHarmonic(N))
|
C#
using System;
class HarmonicNumber {
static double nthHarmonic( int N) {
double [] harmonic = new double [N+1];
harmonic[1] = 1.0;
for ( int i = 2; i <= N; i++) {
harmonic[i] = harmonic[i-1] + 1.0 / i;
}
return harmonic[N];
}
static void Main( string [] args) {
int N = 8;
Console.WriteLine(nthHarmonic(N));
}
}
|
Javascript
function nthHarmonic(N) {
const harmonic = new Array(N + 1);
harmonic[1] = 1.0;
for (let i = 2; i <= N; i++) {
harmonic[i] = harmonic[i - 1] + (1 / i);
}
return harmonic[N];
}
const N = 8;
console.log(nthHarmonic(N));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
20 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...