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:
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:
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.
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.– Woss
Yeah, I don’t know how to make that sum, is that I’m new in programming and I’m a little lost.
– devast