Calculate the arithmetic mean of the integers between 15 (inclusive) and 100 (inclusive)

Asked

Viewed 2,069 times

0

I need to calculate the arithmetic average of the integers between 15 (inclusive) and 100 (inclusive). I made this code:

x=0
for i in range(15,101):
    x=x+i
    print('%d / %d = %5.1f' % (x,i,x/i))

If the overall average would be a little different:

x=0
for i in range(15,101):
   x=x+1
   media=x/85
   print('media geral é %5.2f'%(media))

Good until I have science on the codes of while and for, yet I do not understand what the question really wants. I think the question was very subjective, I did not understand if you want the value of the final average of [15,100] or the average of n+1/n.

3 answers

2

well the arithmetic average is the sum of the intervals in case all the values contained between 15 and 100 divided by the amount of the intervals, your example is erado because vc divides each number by the average what is not requested in your case would be:

soma=0
valor_intervalo=len(range(15,101))

for i in range(15,101):
   soma+=i
media=soma/valor_intervalo

print("media geral {}".format(media)) 

1

"Whole numbers between 15 (inclusive) and 100 (inclusive))" means that are all the numbers on this list: 15, 16, 17, 18... up to 100.

"Arithmetic mean" of a set of numbers is simply the sum of those numbers divided by the amount of numbers.

For example, if it were "arithmetic mean of the integers between 2 (inclusive) and 4 (inclusive)", it would be the arithmetic mean of (2, 3, 4), which would be the sum of them (2 + 3 + 4 = 9) divided by the quantity of numbers (3), then the mean would be 9 / 3 = 3.

In your case, the numbers are (15, 16, 17, .... 99, 100) and the number number is 86 (yes, you can count, there are 85).

Anyway, to do this in Python, you can use a range from 15 to 101 (since a range includes the first number, but does not include the last).

Then you can use sum to calculate the sum of the numbers, and len to get the amount of numbers, and then just divide one by the other:

numeros = range(15, 101)
media = sum(numeros) / len(numeros)
print(media) # 57.5

With this you get the average, which is 57.5.


But since this is an exercise, I believe the intention is that you use ties like the for or while, then you can do this "manually" (although the above solution is much more succinct and simple):

soma = 0
quantidade = 0
for i in range(15, 101):
    soma += i
    quantidade += 1

media = soma / quantidade
print(media)

Or still not using the range:

soma = 0
quantidade = 0
i = 15
while i <= 100:
    soma += i
    quantidade += 1
    i += 1

media = soma / quantidade
print(media)

Note that I only calculate the average at the end (after the for/while), only after the loop is that I have the sum and the amount of numbers.

  • Very cool, but the list conversion is unnecessary - both sum() how much len() work with objects of the type range more efficiently without need "materialize" the list.

  • @nosklo had forgotten that detail. I updated the reply, thank you!

0

To média aritmética of a sequence of values corresponds to the quociente between the soma dos valores and the quantidade dos valores. In other words, the arithmetic mean is the result of the division between a soma dos valores for quantidade de valores.

to resolve this issue we can implement the following code below...

def media(r):
    cont = soma = 0
    for i in r:
        cont += 1
        soma += i
    return soma / cont


if __name__ == '__main__':
    resultado = media(range(15, 101))

    print(f'{resultado:.2f}')

Note that this code was mounted from a function def, from which both the sum of the values and the quantity of values are obtained and then the arithmetic mean is calculated. And finally, the said result is displayed with two decimal places.

Browser other questions tagged

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