Write a python program which will ask user to enter an integer to compute the harmonic number. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Write a python program which will ask user to enter an integer to compute the harmonic number.

If n is positive, your function will return to the sum of first n terms in harmonic series:1/1+1/2+1/3...+1/n. If n is non postive your program will output 0. For example if user entered 4 your program will output something like 2.083333333333 if user entered 7 program will output 2.5928714285

29th Jan 2017, 6:26 PM
Parita Shah
Parita Shah - avatar
5 Answers
+ 1
the harmonic series is the infinite sum over i of 1/i
4th Feb 2017, 1:08 PM
Amaras A
Amaras A - avatar
0
You have to be careful with this one. Do you want maximum precision?
29th Jan 2017, 8:38 PM
Amaras A
Amaras A - avatar
0
what is harmonic series?
29th Jan 2017, 10:46 PM
ramzi
ramzi - avatar
0
No
30th Jan 2017, 12:24 AM
Parita Shah
Parita Shah - avatar
0
Sorry for coming back this late. There is actually a 1e-12 difference between the two ways at n = 10**6 both ways: sum(1/i for i in range(1, n+1)) for MINIMUM precision sum(1/i for i in range(n, 0, -1)) for MAXIMUM precision
3rd Feb 2017, 9:37 PM
Amaras A
Amaras A - avatar