Calculate the sum and mean of a numerical sequence

-4

Following 3,5,7,9,11,13,15. Find 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 average value of 'ALL' values (12 values).

I started like this, then I curled up. Help me.

x = 3
while x < 27:
   print(x)
   x = x + 2
    
asked by anonymous 19.06.2018 / 15:39

3 answers

4
  

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

You've already done the letter (a), but there is room for improvement. In the way you did, you first need to know that to display the 12 elements requested the condition will be x < 27 , but what if you are asked for 20, 50 or even do not know how many? In these cases your solution would not work. The simplest thing is to create a counter that controls how many numbers are displayed:

quantidade = 12
exibidos = 0
numero = 3

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

This will display the 12 numbers in the sequence. If you change% from% to 50, you will show 50. For 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 number of numbers:

media = soma / quantidade

Getting:

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)

Jefferson Quesado

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

Itsatisfiesthesequencegiveninthestatement,butthenexttermwouldbe21(not17).Thesequencewouldbe:3,5,7,9,11,13,15,21,23,25,...

Collaborationof Bacco

Another simpler thing to understand is the sequence given by:

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

19.06.2018 / 16:05
0

Sequence that should be displayed: 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25

Instead of while x < 27 put x <= 25 and also create an auxiliary variable to save the sum of the values.

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

I do not 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++;
    
19.06.2018 / 15:47
0

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

Output:

[3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]
[3]
168
14.0
    
19.06.2018 / 17:05