Calculating Series in Python

Asked

Viewed 1,513 times

2

How to calculate series in Python? I created an algorithm but I’m not getting to the final goal.

Ex:

S1 = x + (x-1) + (x – 2) + ... + (x – n) 
S2 = 1  + 1 +1 + 1 +2 + … 1 

I entered with an initial number and an ending, but the result is giving error. My code:

while True:
    try: 
        value1 = int(input("Digite o número inicial: "))
        value2 = int(input("Digite o numero final: "))
        for i in range(value1,value2):
            num1 = value1 +(value1-1)+(value1-2)+(value1 - value2)
            i += 1
            print("Valor é: ",num1)
            break
        break
    except ValueError: 
        print("Obs: Somente números inteiros! Tente novamente...\n")

2 answers

2

The first series:

S1 = x + (x-1) + (x – 2) + ... + (x – n) 

Can be simplified so that the calculation is O(1) by grouping the terms in common:

S1 = x + (x-1) + (x – 2) + ... + (x – n) 
   = (x + x + x + ... + x) - (0 + 1 + 2 + ... + n)
   = (n+1)*x - n*(n+1)/2
   = (n+1)*(x - n/2)

That is, the result can be obtained only with some mathematical operations, 4 precisely, regardless of the magnitude of n, without having to implement some looping loop that would leave the solution at least O(n).

In Python, this could be implemented even in a lambda expression:

S1 = lambda x, n: (n+1)*(x - n/2)

For example, S1(5, 3) will result in 14 as:

S1 = 5 + (5 - 1) + (5 - 2) + (5 - 3)
   = 5 + 4 + 3 + 2
   = 14

As a curiosity, the solution with loop repetition could be:

S1 = lambda x, n: sum(x-i for i in range(n+1))

As for series 2, I was unable to understand the notation used.

  • Apparently the S2 is S2 = lambda x, n: (n+1)*(x +n/2); as if it were the series 1 only, instead of subtracting x by something, it adds x with that same something

  • @Jeffersonquesado at first seemed, but this factor 1 that appears in all terms generated me strangeness. I believe it would be a fraction: 1/x + 1/(x+1) + 1/(x+2) + ...

1

According to your serial example and using the same variables (x and n):

serie = 0
x = int(input("Digite o valor de x: "))
n = int(input("Digite o valor de n: "))

for i in range(1,n + 1):
    serie += x - i
    print("\nValor da serie +=", x, "-", i, "=", x - i)
    i += 1
print("\nValor da serie total =", serie)

Behold functioning on the ideone

To make the S2, just invert the subtractions for additions.

  • Not getting the correct amount @Gabriel Ranéa Barbosa, but your help helped me too much, man. The calculation should be done with an initial value and a final value, Ex: starts the series calculation at number 2 and goes up to number 5. But your explanation is great, thank you.

  • Would "x" or "n" be 2 to 5? If it is "x" you can ask X1 and x2 from the user and do another for before that to cover the range you want: for x in range(x1, x2) :

  • If the answer helped you evaluate to help other users too.

  • Yes, thank you very much !!

  • I invite you to do the [tour] (http://answall.com/tour) to learn more about the site and how to evaluate responses, among other things ;)

Browser other questions tagged

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