Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

finding sum of fractions n/1 to 1/n

I am trying to find the sum n/1 + (n-1)/2 + (n-2)/3 ... + 1/n. I am not getting the correct output

This is what I have

n = int(input("Please enter a positive integer: "))

sum2 = 0.0

for i in range(1, n-1):
    sum2 = sum2 + (i/1)

print("For n =", n, "the sum n/1 + (n-1)/2 + ... 1/n is", sum2)

My expected output for sum2 is 11.15 when 6 is entered as n but it’s not correct. What am I doing wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

When talking about the 2nd summation, besides the numerator decreasing one by one, the denominator also needs to increase one by one.

n = int(input("Please enter a positive integer: "))


sum2 = 0

for i in range(0, n):
    sum2 = sum2 + (n-i)/(i+1)

print("For n =", n, "the sum n/1 + (n-1)/2 + ... 1/n is", sum2)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading