Operation with Factorials from thehuxley.com website

Asked

Viewed 891 times

0

inserir a descrição da imagem aqui

Input format: An integer x corresponding to the X of the equation and an integer n indicating a number of series terms, input ends when x = 0 and n = 0.

Output format: A floating point number formatted with six decimal places.


Where’s the mistake? I don’t know if I’m doing it right. By submitting the question on the website... I get the following message:
"The evaluation result was 'WRONG_ANSWER' which means your program did not return the expected response."

I do not know if it is problem of the system of the site, I had problems in a similar matter, depending on the language C, Pascal, Java, Python and the type used to treat the floating point (float, double, long double) the result could give slightly different.

def fatorial(number):
    if number < 1:
        return 1
    else:
        return number * fatorial(number - 1)

n = input().split(" ")

while int(n[0]) != 0 and int(n[1]) != 0:
    valor = int(n[0])
    soma = False 
    for number in range(3, int(n[1]) + int(n[1]) + 2, 2):
        if(soma != True):  # SOMA
            valor -= int(n[0]) ** (number - 1) / fatorial(number)
            soma = True
        else:
            valor += int(n[0]) ** (number - 1) / fatorial(number)
            soma = False

    print("%.6f" % valor)        
    n = input().split(" ")


The problem was that it was not counting the "X" as a term of the series... So when asked 5 terms it was actually 6...

Correct algorithm:

def fatorial(number):
    if number < 1:
        return 1
    else:
        return number * fatorial(number - 1)

n = input().split(" ")
resultado = []
while int(n[0]) != 0 or int(n[1]) != 0:
    valor = float(n[0])
    pot = 0
    if(int(n[1]) != 0):
        for number in range(1, int(n[1])):
            pot += 2
            if(number % 2 != 0):  # IMPAR
                valor -= int(n[0]) ** (pot) / fatorial(pot + 1)
            else:
                valor += int(n[0]) ** (pot) / fatorial(pot + 1)
        print(format(valor, ".6f"))
    else:
        print(format(0, ".6f"))  

    n = input().split(" ")
  • 1

    Hello. Considering what was answered there in your other question, did you come to compare the output of your program with the result of manual tests (or using the help of tools like Excel)? I didn’t think it was right to mark this question as duplicate, but sometimes the problem is the same as before. :)

1 answer

2

To realize the sum of the elements of the series it is not necessary to calculate factorial nor powers, just keep control variables for this.

For example, 3! = 3 x 2 x 1 and 5! = 5 x 4 x 3 x 2 x 1, soon 5! = 5 x 4 x 3!.

The same can be applied for exponents of X: X4 = X2 x X2 and X6 = X4 x X2.

Therefore, to calculate the factorial you must multiply the previous factorial by the next two numbers and in the case of the exponent just multiply the previous result by X2 at each iteration.

With all this in mind, I’ve assembled the following algorithm:

def calcular_serie(x, n):
    soma = x if n > 0 else 0
    sinal = -1
    exponencial = quadrado = x * x
    fatorial = 6
    for i in range(2, n + 1):
        soma += sinal * exponencial / fatorial
        sinal = -sinal;
        exponencial *= quadrado
        fatorial *= (i * 2) * (i * 2 + 1)
    return soma

So I tested it with the following excerpt, which you can also use if you want to test your:

print calcular_serie(10000, 0) #esperado = 0
print calcular_serie(0, 1) #esperado = 0
print calcular_serie(0, 2) #esperado = 0
print calcular_serie(1, 1) #esperado = 1
print calcular_serie(1, 2) #esperado = 1 - 1/6 = 0.83333
print calcular_serie(1, 3) #esperado = 1 - 1/6 + 1 / 120 = 0.8416663
print calcular_serie(2, 1) #esperado = 2
print calcular_serie(2, 2) #esperado = 2 - 4/6 = 1.33333
print calcular_serie(2, 3) #esperado = 2 - 4/6 + 16 / 120 = 1.46666666

Functional demonstration in repl.it

Note that during the printing of the numbers there may be differences in the printing. For example, one of the outputs displays 0.0 because the result was a floating point.

Depending on how the result validation site is implemented, you may have to handle that output.

Browser other questions tagged

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