Python script to calculate a series

Asked

Viewed 46 times

0

Data n, print the value of

Sn = 1/n + 2/(n-1) + 3/(n-2) + ... + n.

This is what I tried to do:

n =2
soma =1
for i in range(1,n+1):
    if n>i:        
        soma+=i/n + i/(n-1)

But it only works when n=2. Someone could help?

  • 1

    I’m sorry for the ignorance, but this sequence is strange. It implies that the values will eventually converge on n, but in fact they will converge on n+1/(n-n), in which you will get a division error by 0. This sequence is right?

  • @Andre withdrew from an exercise; https://www.ime.usp.br/~Cris/lessons/08_1_110/abril.html

  • @Andre pulled from one exercise - Problem 10.1:; https://www.ime.usp.br/~Cris/lessons/08_1_110/abril.html

  • 1

    Well, in the exercise itself has an example of C implementation, which converted to Python would be thus, I guess you can use it as a reference, it’s your tie for that is doing more thing.

  • @Andre did not understand (i+1)/(n-i)

  • 1

    It’s the general term of your series

  • @Woss really did not understand how this general term was arrived at

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.