Calculate the sum and mean of a numerical sequence

Asked

Viewed 1,369 times

-4

In sequence 3,5,7,9,11,13,15. Discover the pattern of the sequence and:

  • a) Make a program to display 12 values of this sequence started by 3.
  • b) Show the sum of 'ALL' values (12 values).
  • c) Show the mean value of 'ALL' values (12 values).

I started out like this, so I curled up. Help me.

x = 3
while x < 27:
   print(x)
   x = x + 2
  • Already got the letter (a), but had difficulties in the others? If you need the sum of all the values, it would not be enough to add each value of x? And the required average in (c) is the division between the sum and the amount of elements.

  • Yeah, I don’t know how to make that sum, is that I’m new in programming and I’m a little lost.

3 answers

4


Note: it is important to note that there are infinite number series that satisfy the sequence {3,5,7,9,11,13,15,...} (view end of reply), then ask the user to identify the standard makes no sense and especially if any of the series were implemented, it would be a valid solution. The solution a_n = 2*n + 1 is possibly the most trivial of them and, probably for that reason, will be the one expected by the teacher.

You have already done the letter (a), but there is a way to improve. The way you did, you need to initially know that to display the 12 requested elements the condition will be x < 27, But what if you’re asked for 20, 50 or even don’t even know how many? Then your solution wouldn’t work. The simplest is to create a counter that controls how many numbers will be displayed:

quantidade = 12
exibidos = 0
numero = 3

while exibidos < quantidade:
    print(numero)
    numero += 2
    exibidos += 1

Thus, the 12 numbers in the sequence will be shown. If you change quantidade to 50, will show the 50. Already, for the letter (b), you need to accumulate the sum in another variable:

quantidade = 12
exibidos = 0
numero = 3
soma = 0

while exibidos < quantidade:
    print(numero)
    soma += numero
    numero += 2
    exibidos += 1

print('Soma = ', soma)

And finally, the average will be the division between the sum and the amount of numbers:

media = soma / quantidade

Staying:

quantidade = 12
exibidos = 0
numero = 3
soma = 0

while exibidos < quantidade:
    print(numero)
    soma += numero
    numero += 2
    exibidos += 1

print('Soma = ', soma)

media = soma / quantidade

print('Média = ', media)

Collaboration of Jefferson Quesado

As we are dealing with numerical sequences, the continuity of the generating function is not a requirement, so, for example, the sequence generated by:

inserir a descrição da imagem aqui

It satisfies the sequence given in the statement, but the following term would be 21 (not 17). The sequence would be: 3, 5, 7, 9, 11, 13, 15, 21, 23, 25, ...

Collaboration of Bacchus

Another much simpler to understand is the sequence given by:

inserir a descrição da imagem aqui

That is, calculate the rest of the 2x+1 division by 17, so the generated sequence would be: 3, 5, 7, 9, 11, 13, 15, 0, 2, 4, 6, ..., which also satisfies the sequence given in the statement, but the following term is not 17.

  • Brother Brawler, that’s right, I’m going to study the code...

0

Sequence to be shown: 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25.

In place of while x < 27 place x <= 25 and also create an auxiliary variable to store the sum of values.

Example: y = 0 (start it with 0 so there is no dirt left over from the sums and give wrong values).

I don’t know how to do it in Phyton, I made an example using Java logic.

x = 3;
y = 0;

   while x < 27:
       print(x)
         y = x + 2;
         x++;
  • 1

    And what’s the difference between the condition x <= 25 and x < 27, whereas x will be unique?

  • Oh sorry for the mistake! I hadn’t noticed it. I believe using x <= 25 can be of better understanding (at least for me, it is. It has been two years since I started with programming, and the clearer and obvious every logic/ variable that I use, the understanding to my point of view is better.). You tested using an auxiliary variable?

  • 1

    I was the one who made a mistake, it was x <= 25 and he has to show these values 3 5 7 9 11 13 15 17 19 21 23 25 The sum of all these numbers is 168

  • Try to make an auxiliary variable to perform the sum of the values and then just divide by 12 (which is the amount of elements) to make your average.

  • Maria Clara you could show how it is?

  • I don’t know how to program in Phyton. But I will use the same logic with JAVA, can it be? It would be more or less: y = x + 2 there, while the x <=25 it adds up (when used x++, it accumulates until arriving the result that was defined by you, that in this case, would be x <= 25). After he adds up all the values, you print the Y. After printar, you do that math calculation: y / 2 (average calculation) and printa Y again.

Show 1 more comment

0

Follows the solution:

# Geracao da sequencia contendo 12 valores
seq = list(range(3, 27, 2))

# Extrai valores da sequencia iniciados com '3'
a = [ n for n in seq if str(n)[0] == '3' ]

# Calcula soma dos valores da sequencia
b = sum(seq)

# Calcula a media dos valores da sequencia 
c = sum(seq) / float(len(seq))

print(seq)
print(a)
print(b)
print(c)

Exit:

[3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]
[3]
168
14.0
  • 1

    Using range, would not be simpler list(range(3, 27, 2))? And could explain what the object would be a?

  • a, b and c are the answers to the respective question items. a these are the valores da sequência iniciado por 3.

  • Ah, yes, but I believe seq would already be the answer of a xD

  • @Andersoncarloswoss: Or is it that show 12 values of this sequence started by 3 wouldn’t be something like: print([ int('3' + str(n)) for n in seq ]) ?!?!

Browser other questions tagged

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