如果序列的逆遵循AP的规则,即算术级数,则称其为谐波级数。通常,谐波级数中的项可以表示为: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”是共同的区别。
我们可以使用for循环来找到总和。
C++
// C++ program to find sum of series
#include
using namespace std;
// Function to return sum of
// 1/1 + 1/2 + 1/3 + ..+ 1/n
class gfg
{
public : 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()
{
gfg g;
int n = 5;
cout << "Sum is " << g.sum(n);
return 0;
}
// This code is contributed by SoM15242.
C
// C program to find sum of series
#include
// Function to return sum of 1/1 + 1/2 + 1/3 + ..+ 1/n
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 series
import java.io.*;
class GFG {
// Function to return sum of
// 1/1 + 1/2 + 1/3 + ..+ 1/n
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));
}
}
// This code is contributed by Nikita Tiwari.
Python3
# Python program to find the sum of 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))
# This code is contributed by Chinmoy Lenka
C#
// C# Program to find sum of series
using System;
class GFG {
// Function to return sum of
// 1/1 + 1/2 + 1/3 + ..+ 1/n
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));
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出:
2.283333