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

I need to calculate the arithmetic mean of 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 it's the overall average it 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 I even have knowledge about the codes of while and for, however I did not understand what the question really wants. I think the question was very subjective, I did not understand if either the value of the final mean of [15,100] or the mean of n + 1 / N.

Author: hkotsubo, 2019-03-24

3 answers

Well the arithmetic mean is the sum of the intervals in the case all the values contained between 15 and 100 divided by the amount of the intervals, your example is erado because you divide each number by the mean what is not required 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)) 
 2
Author: , 2019-03-24 16:59:55

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

"arithmetic mean" of a set of numbers is simply the sum of these numbers divided by the amount of numbers.

For example, if it were " arithmetic mean of 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 amount of numbers (3), then the average would be 9 / 3 = 3.

In your case, the numbers are (15, 16, 17, .... , 99, 100) and the amount of numbers is 86 (yes, you can count, they are not 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 for 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 loops like for or while, so 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 without using range:

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

media = soma / quantidade
print(media)

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

 1
Author: hkotsubo, 2019-03-25 16:48:36

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

To solve this issue we can implement the following code below...

cont = soma = 0
for c in range(15, 101):
    cont += 1
    soma += c

media = (soma / cont)
print(f'\033[32mA média é: {media:.2f}\033[m')

See here how the algorithm works.

 0
Author: Solkarped, 2020-07-11 21:26:35