📜  找出谐波序列之和的程序

📅  最后修改于: 2021-04-23 16:50:52             🧑  作者: Mango

调和级数与算术级数成反比。通常,谐波级数中的项可以表示为1 / a,1 /(a + d),1 /(a + 2d),1 /(a + 3d)…。 1 /(a + nd)。
由于AP的N项为(a +(n – 1)d)。因此,谐波阶跃的第N个项是AP的第N项的倒数,即1 /(a +(n – 1)d),其中“ a”是AP的第一个项,而“ d”是一个共同的区别。

方法1:简单方法

C++
// C++ program to find sum of harmonic series
#include
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 
 
// 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


Javascript


C++
// CPP program to find sum of
// harmonic series using recursion
#include
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


Javascript


输出:
Sum is 2.283333

方法2:使用递归

C++

// CPP program to find sum of
// harmonic series using recursion
#include
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


Java脚本


输出:
2.7178571428571425
2.9289682539682538