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.