Python | sympy.harmonic() method

With the help of sympy.harmonic() method, we can find Harmonic numbers in SymPy.

harmonic(n)

The nth harmonic number is given by – \operatorname{H}_{n} = 1 + \frac{1}{2} + \frac{1}{3} + \ldots + \frac{1}{n}.

Syntax: harmonic(n)

Parameter:
n – It denotes the number upto which harmonic number is to be calculated.

Returns: Returns the nth harmonic number.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# import sympy 
from sympy import * 
  
n = 7
print("Value of n = {}".format(n))
   
# Use sympy.harmonic() method 
nth_harmonic = harmonic(n)  
      
print("Value of nth harmonic number : {}".format(nth_harmonic))  

chevron_right


Output:

Value of n = 7
Value of nth harmonic number : 363/140
harmonic(n, m)

The nth generalized harmonic number of order m is given by – \operatorname{H}_{n, m} = \sum_{k=1}^{n} \frac{1}{k^m}.

Syntax: harmonic(n, m)

Parameter:
n – It denotes the number upto which harmonic number is to be calculated.
m – It denotes the order of the harmonic number.
Returns: Returns the nth harmonic number of order m.

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# import sympy 
from sympy import * 
  
n = 5
m = 2
print("Value of n = {} and m = {}".format(n, m))
   
# Use sympy.harmonic() method 
nth_harmonic_poly = harmonic(n, m)  
      
print("The nth harmonic number of order m : {}".format(nth_harmonic_poly))  

chevron_right


Output:

Value of n = 5 and m = 2
The nth harmonic number of order m : 5269/3600


My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.