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?
>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)