Programa para hallar la suma de series armónicas

La serie armónica es inversa a una progresión aritmética . En general, los términos en una progresión armónica se pueden denotar como 1/a, 1/(a + d), 1/(a + 2d), 1/(a + 3d)…. 1/(a + nd). 
Como el N -ésimo término de AP se da como (a + (n – 1)d). Por lo tanto, el término N de progresión armónica es recíproco del término N de AP, que es 1/(a + (n – 1)d), donde “a” es el primer término de AP y “d” es una diferencia común.

Método #1: Enfoque simple 

C++

// C++ program to find sum of harmonic series
#include<bits/stdc++.h>
using namespace std;
  
// Function to return sum of harmonic series
double sum(int n)
{
  double i, s = 0.0;
  for(i = 1; i <= n; i++)
      s = s + 1 / i;
       
  return s;
}
 
// Driver code
int main()
{
    int n = 5;
     
    cout << "Sum is " << sum(n);
    return 0;
}
 
// This code is contributed by SHUBHAMSINGH10

C

// C program to find sum of harmonic series
#include <stdio.h>
 
// Function to return sum of harmonic series
double sum(int n)
{
  double i, s = 0.0;
  for (i = 1; i <= n; i++)
      s = s + 1/i;
  return s;
}
 
int main()
{
    int n = 5;
    printf("Sum is %f", sum(n));
    return 0;
}

Java

// Java Program to find sum of harmonic series
import java.io.*;
 
class GFG {
     
    // Function to return sum of
    // harmonic series
    static double sum(int n)
    {
      double i, s = 0.0;
      for (i = 1; i <= n; i++)
          s = s + 1/i;
      return s;
    }
  
    
    // Driven Program
    public static void main(String args[])
    {
        int n = 5;
        System.out.printf("Sum is %f", sum(n));       
    }
}

Python3

# Python program to find the sum of harmonic series
 
def sum(n):
    i = 1
    s = 0.0
    for i in range(1, n+1):
        s = s + 1/i;
    return s;
 
# Driver Code
n = 5
print("Sum is", round(sum(n), 6))

C#

// C# Program to find sum of harmonic series
using System;
 
class GFG {
     
    // Function to return sum of
    // harmonic series
    static float sum(int n)
    {
        double i, s = 0.0;
         
        for (i = 1; i <= n; i++)
            s = s + 1/i;
             
        return (float)s;
    }
 
     
    // Driven Program
    public static void Main()
    {
        int n = 5;       
        Console.WriteLine("Sum is "
                           + sum(n));       
    }
}

PHP

<?php
// PHP program to find sum of harmonic series
 
// Function to return sum of
// harmonic series
function sum( $n)
{
    $i;
    $s = 0.0;
    for ($i = 1; $i <= $n; $i++)
        $s = $s + 1 / $i;
    return $s;
}
 
    // Driver Code
    $n = 5;
    echo("Sum is ");
    echo(sum($n));
     
?>

Javascript

<script>
// JavaScript program to find sum of harmonic series
 
// Function to return sum of harmonic series
function sum(n)
{
let i, s = 0.0;
for(i = 1; i <= n; i++)
    s = s + 1 / i;
         
return s;
}
 
// Driver code
let n = 5;
document.write("Sum is " + sum(n));
 
// This code is contributed by Surbhi Tyagi.
</script>
Producción: 

Sum is 2.283333

 

Complejidad de tiempo: O (n), ya que estamos atravesando una vez en la array.

Espacio auxiliar: O(1), no se necesita espacio adicional.

Método #2: Usar recursividad 

C++

// CPP program to find sum of
// harmonic series using recursion
#include<bits/stdc++.h>
using namespace std;
 
float sum(float n)
{
    // Base condition
    if (n < 2)
        return 1;
 
    else
        return 1 / n + (sum(n - 1));
}
 
// Driven Code
int main()
{
    cout << (sum(8)) << endl;
    cout << (sum(10)) << endl;
    return 0;
}
 
// This code is contributed by
// Shashank_Sharma

Java

// Java program to find sum of
// harmonic series using recursion
import java.io.*;
 
class GFG
{
 
float sum(float n)
{
    // Base condition
    if (n < 2)
        return 1;
 
    else
        return 1 / n + (sum(n - 1));
}
 
// Driven Code
public static void main(String args[])
{
  GFG g = new GFG();
  System.out.println(g.sum(8));
  System.out.print(g.sum(10));
}
}
 
// This code is contributed by Shivi_Aggarwal

Python3

# Python program to find sum of
# harmonic series using recursion
 
def sum(n):
 
    # Base condition
    if n < 2:
        return 1
 
    else:
        return 1 / n + (sum(n - 1))
         
print(sum(8))
print(sum(10))

C#

//C# program to find sum of
// harmonic series using recursion
using System;
 
class GFG
{
 
static float sum(float n)
{
    // Base condition
    if (n < 2)
        return 1;
 
    else
        return 1 / n + (sum(n - 1));
}
 
// Driven Code
public static void Main()
{
    Console.WriteLine(sum(8));
    Console.WriteLine(sum(10));
}
}
 
// This code is contributed by shs..

PHP

<?php
// PHP program to find sum of
// harmonic series using recursion
 
function sum($n)
{
 
    // Base condition
    if ($n < 2)
        return 1;
 
    else
        return 1 / $n + (sum($n - 1));
}
 
// Driver Code
echo sum(8) . "\n";
echo sum(10);
 
// This code is contributed by Ryuga
?>

Javascript

<script>
 
// Javascript program to find sum of
// harmonic series using recursion
function sum(n)
{
     
    // Base condition
    if (n < 2)
    {
        return 1
    }
    else
    {
        return 1 / n + (sum(n - 1))
    }
}
  
// Driver code
document.write(sum(8));
document.write("<br>");
document.write(sum(10));
 
// This code is contributed by bunnyram19
  
</script>
Producción: 

2.7178571428571425
2.9289682539682538

 

Complejidad de tiempo: O(n), ya que recurrimos n veces.

Espacio auxiliar: O(n), debido al espacio de pila recursivo.

Publicación traducida automáticamente

Artículo escrito por aishwarya.27 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *