How to count how many odd numbers there are between -999 and 0

Asked

Viewed 682 times

-2

I need to make an algorithm that tells me how many odd numbers there are between -999 and 0

Example: between these values a n odd numbers

2 answers

1

This is an arithmetic progression exercise also known as PA.

An arithmetic progression is a numerical sequence in which each term, starting from the second, is equal to the sum of the previous term with a constant r. The number r is called the common ratio or difference of arithmetic progression.

The formula of the general term of an Arithmetic Progression is given by:

an = a1 + (n - 1). r

where:

an = last term of the sequence.

a1 = first term of the sequence.

n = number of terms in sequence.

r = reason for the sequence.

For this question we will use the formula of the quantity of terms of a PA that is determined by:

n = (an - a1)/r + 1

Then to get the amount of prime numbers between -999 and 0 just replace the known values in the formula and find n.

With to get a new odd number we must add 2 to the previous odd:

1 + 2 = 3

3 + 2 = 5

5 + 2 = 7

Implying that reason of its sequence, or r, is 2. As you want to calculate the amount of odd numbers the considered series should start with the first number and the last odd in its sequence. Then:

an = -1

a1 = -999

r = 2

Therefore:

n = (-1 - (-999))/2 + 1 -> n = 998/2 + 1 -> n = 499 + 1 => n = 500

Applying this reasoning in Python:

#Lê o primeiro termo da PA
a1 = int(input("Digite o valor do primeiro termo: "))
#lê o último termo da PA
an = int(input("Digite o valor do último termo: "))

#Verifica se a sequencia é crescente ou decrescente 
#e procura o primeiro e último ímpar do intervalo
if (an >= a1):
  if (a1 % 2 == 0): a1 += 1
  if (an % 2 == 0): an -= 1
  #Ajusta a razão pra série crescente
  r = 2
else:
  if (a1 % 2 == 0): a1 -= 1
  if (an % 2 == 0): an += 1
  #Ajusta a razão pra série decrescente
  r = -2

#Calcula o número de termos da PA
n = (an - a1)//r + 1

#Exibe o resultado
print('Existem ' + str(n) + ' ímpar(es) nesse intervalo.')

Code working Repl.it

0

This is a basic exercise, practice.

c = 0
for i in range(-999, 0):
    if i % 2 != 0:
        c += 1
print(c)

########OUTPUT########
500
  • the correct would be: for i in range(-999, 1):

  • 2

    @Luizaugusto As zero is even, whether he is in or not range (if it were an algorithm that gets any numbers, in fact the right ones would be range(inicio, fim + 1), but as he asked for specific numbers, it makes no difference). But actually, if I only want the odd ones, use range(-999, 0, 2) to skip the pairs. But if this range only has odd ones, why test % 2? Hurry up len(range(-999, 0, 2)) :-) Okay, I know that probably is an exercise and the teacher probably want to make the loop "traditional" one in one, but I thought it was worth mentioning the alternatives... :-)

  • @exact hkotsubo, then I noticed that 1 or 0 would make no difference to the proposed exercise, but I think it was worth mentioning since the teacher might ask something like.

Browser other questions tagged

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