As already mentioned in other answers this code does not bother to affirm the validity or not of the user input.
n = int(input('Digite um número inteiro: '))
g = (n - n % 2 ) // 2 #Calcula a geratriz de n usando sua paridade.
print([2 * i + 1 for i in range(g, g + 6)]) #Computa os seis próximos ímpares usando a geratriz.
Test this example on Repl.it
Some concepts to help understand the algorithm:
Parity
Parity is a notion applied to the set of integers where any integer is said to be even if being divided by the number two results in an integer, otherwise that number is said to be odd. Being able to affirm:
- for a pair the rest of your entire division by 2 will always be 0.
- for an odd the rest of your entire division by 2 will always be 1.
Pairs and Odd Sets
The set of even numbers is the set of integers divisible by two, i.e., the division by two is closed in the set of integers. This set is described by the function:
nₚ = 2 * n | n ∈ Z
the even number is equal twice the number n such that n belongs to the integers
In turn, the set of odd numbers is the set of integers not divisible by two, i.e., the division by two is opened in the set of integers. This set is described by the function:
nₗ = 2 * n + 1 | n ∈ Z
the odd number is equal to twice n plus 1 such that n belongs to the integers
Geratrizes
Geratriz is what generates or gives rise to parity we can say that:
- The even number generator, the number n that generated nₚ
gₚ = nₚ / 2
even geratriz equals even number divided by 2
- The odd number generator, the number n that generated nₗ:
gₗ = ( nₗ - 1 ) / 2
the odd geratriz is equal to the odd number minus one divided by 2
If we apply the notion of parity and terms %
as the remaining operator of the entire division, we can generalize the generator calculation as follows:
g = ( n - n % 2 ) / 2 | n ∈ Z
the parity geratriz of an integer is equal to the division by two of n minus the remainder of its division by 2, such that n belongs to the integers
This problem is set right? Because I can’t imagine why it should be so complicated.
– Maniero
If you’ve already guaranteed that x is odd (adding 1 when it’s even, for example), and is increasing by 2 by 2, there’s no point testing again inside the
for
– hkotsubo
Not questioning the content of the answer accepted (I myself upvoted the answer) and not questioning choice, but I saw that you marked how you accept all the answers by clicking on the " " of all of them. See in how and why to accept an answer, the operation of the response acceptance tool. In summary you can vote on all answers but must accept only one.
– Augusto Vasques