Dado un número N. La tarea es encontrar el N-ésimo Número Armónico.
Deje que el n-ésimo número armónico sea Hn .
La serie armónica es la siguiente:
H 1 = 1
H 2 = H 1 + 1/2
H 3 = H 2 + 1/3
H 4 = H 3 + 1/4
.
.
.
H norte = H n -1 + 1/n
Ejemplos :
Input : N = 5 Output : 2.45 Input : N = 9 Output : 2.71786
La idea es atravesar desde H1 y luego seguir encontrando consecutivamente H2 desde H1, H3 desde H2… y así sucesivamente.
A continuación se muestra el programa para encontrar el N-ésimo número armónico:
C++
// CPP program to find N-th Harmonic Number #include <iostream> using namespace std; // Function to find N-th Harmonic Number double nthHarmonic(int N) { // H1 = 1 float harmonic = 1.00; // loop to apply the formula // Hn = H1 + H2 + H3 ... + Hn-1 + Hn-1 + 1/n for (int i = 2; i <= N; i++) { harmonic += (float)1 / i; } return harmonic; } // Driver Code int main() { int N = 8; cout<<nthHarmonic(N); return 0; }
Java
// Java program to find N-th Harmonic Number import java.io.*; class GFG { // Function to find N-th Harmonic Number static double nthHarmonic(int N) { // H1 = 1 float harmonic = 1; // loop to apply the formula // Hn = H1 + H2 + H3 ... + Hn-1 + Hn-1 + 1/n for (int i = 2; i <= N; i++) { harmonic += (float)1 / i; } return harmonic; } // Driver Code public static void main (String[] args) { int N = 8; System.out.print(nthHarmonic(N)); } } // This code is contributed // by ajit
Python 3
# Python3 program to find # N-th Harmonic Number # Function to find N-th Harmonic Number def nthHarmonic(N) : # H1 = 1 harmonic = 1.00 # loop to apply the formula # Hn = H1 + H2 + H3 ... + # Hn-1 + Hn-1 + 1/n for i in range(2, N + 1) : harmonic += 1 / i return harmonic # Driver code if __name__ == "__main__" : N = 8 print(round(nthHarmonic(N),5)) # This code is contributed by ANKITRAI1
C#
// C# program to find N-th Harmonic Number using System; class GFG { // Function to find N-th Harmonic Number static double nthHarmonic(int N) { // H1 = 1 float harmonic = 1; // loop to apply the formula // Hn = H1 + H2 + H3 ... + // Hn-1 + Hn-1 + 1/n for (int i = 2; i <= N; i++) { harmonic += (float)1 / i; } return harmonic; } // Driver Code static public void Main () { int N = 8; Console.Write(nthHarmonic(N)); } } // This code is contributed // by Raj
PHP
<?php // PHP program to find // N-th Harmonic Number // Function to find N-th // Harmonic Number function nthHarmonic($N) { // H1 = 1 $harmonic = 1.00; // loop to apply the formula // Hn = H1 + H2 + H3 ... + // Hn-1 + Hn-1 + 1/n for ($i = 2; $i <= $N; $i++) { $harmonic += (float)1 / $i; } return $harmonic; } // Driver Code $N = 8; echo nthHarmonic($N); // This code is contributed // by Shivi_Aggarwal ?>
Javascript
<script> // Javascript program to find // N-th Harmonic Number // Function to find N-th // Harmonic Number function nthHarmonic(N) { // H1 = 1 let harmonic = 1.00; // loop to apply the formula // Hn = H1 + H2 + H3 ... + // Hn-1 + Hn-1 + 1/n for (let i = 2; i <= N; i++) { harmonic += parseFloat(1) / i; } return harmonic; } // Driver Code let N = 8; document.write( nthHarmonic(N).toFixed(5)); // This code is contributed by bobby </script>
Producción:
2.71786
Complejidad de tiempo : O(N)
Publicación traducida automáticamente
Artículo escrito por Shashank_Pathak y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA