Program in python regarding the factor of odd numbers up to the one typed by the user

Asked

Viewed 629 times

2

I must ask the user to enter an odd number and return on the screen the sum of the factorial of all the odd numbers until the one typed by the user.

I did the part that validates if the number typed is odd and the function that calculates the factorial of the number typed. But I don’t know how to do the rest. I couldn’t even think of the logic of the other function. I made this code:

def fatorial(x):
    result=1
    for i in range(x):
        result=result*(x-i)    
    return result

'''def fatorial(x):
    x=x-2
    while x>1:
        fat=fat*a
        a-=1 - Não deu certo'''
a=int(input("Digite o número: "))
while a%2!=1:
    a=int(input("Número não impar, digite outro número: "))
    while a<0:
        a=int(input("Número menor que zero, digite outro número: "))
else:
    b=fatorial(a)
print(b)

1 answer

2


Having the function that calculates the factorial and the number typed N, just make a for by the odd numbers from 1 to N, and adding up their factorial:

def fatorial(x):
    result = 1
    for i in range(2, x + 1):
        result *= i
    return result

while True:
    n = int(input('digite o número'))
    if n % 2 == 0 or n <= 0:
        print('número deve ser impar e maior que zero, digite outro')
    else: break

soma = 0
for i in range(1, n + 1, 2):
    soma += fatorial(i)

print(soma)

Note that to check if the number typed is odd I made a loop infinite (while True), which is only interrupted if the number is odd (if it is even, print the message and ask it to type again). I also included checking whether it is negative, as it seems only positive.

Then I made a for using a range, which starts at 1 and goes up to n, skipping 2 by 2 (so I know it will only go through the odd ones until n). I had to put n + 1 because the final value is not included in range.

I understand that because it’s an exercise, can be who want you to go 1 in 1 and test if the number is odd, something like that:

for i in range(1, n + 1):
    if i % 2 != 0:
        soma += fatorial(i)

But honestly, unless the exercise requires you to iterate 1-in-1 and use the operator %, this is unnecessary, because if I start from an odd number and just want to iterate by the odd ones, just jump from 2 to 2.

See also that I changed the function that calculates the factorial, to start the range with 2 (starting with 1 is unnecessary because multiplying by 1 is redundant). The way you did it even works, but in a range it is possible to control the initial and final value, so I think it looks better this way. If n for 1, it doesn’t even enter the for and the result is 1.

Oh yes, you said you were trying to do a function to calculate the sum. In this case it would be:

def soma_fat(n):
    soma = 0
    for i in range(1, n + 1, 2):
        soma += fatorial(i)
    return soma

n = # ler valor do n
print(soma_fat(n))

If you want, you can also calculate the sum using sum along with a Generator Expression, much more succinct and pythonic:

soma = sum(fatorial(i) for i in range(1, n + 1, 2))

It is also possible to give a small "optimized". When calculating the factorial of 3, for example, we multiply 1 * 2 * 3, and then we have to calculate the factorial of 5, making 1 * 2 * 3 * 4 * 5 (that is, the first multiplications will be done again).

You can avoid this rework keeping the factorial of the last calculated number, and only do the multiplications that are missing:

# ler n usando o while True acima

soma = 1
fat = 1
for i in range(3, n + 1, 2):
    fat *= i * (i - 1)
    soma += fat
print(soma)

If n is less than 3 (i.e., if it is 1), it does not even enter the for and the result is 1. If greater than or equal to 3, the loop updating the factorial (multiplying the missing values) and adding to the total.

  • 1

    Man, very good. Thank you for the explanations and the detail.

Browser other questions tagged

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