Operation with Factors of thehuxley.com site

0

Inputformat:AnintegerxcorrespondingtotheXoftheequationandanintegernindicatingthenumberoftermsintheseries,theinputendswhenx=0andn=0.

OutputFormat:Afloating-pointnumberformattedtosixdecimalplaces.

Whereistheerror?IdonotknowifI'mdoingitright.Bysubmittingthequestiononthesite...Igetthefollowingmessage:
"The result of the evaluation was 'WRONG_ANSWER' which means that your program did not return the expected response."

I do not know if it is a problem with the site system, because I had problems in a similar question , depending on the language C, Pascal, Java, Python and the type used to treat 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 he was not counting the "X" as a term in the series ... So when he was asked 5 terms he actually did 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(" ")
    
asked by anonymous 07.06.2014 / 04:59

1 answer

2

To perform the sum of the elements of the series it is not necessary to calculate factorial or 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 , then 5! = 5 x 4 x 3! .

The same can be applied to the exponents of X : X 4 = X 2 x X 2 and X 6 = X 4 x X 2 .

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

With all of this in mind, I've put together 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 snippet, which you can also use if you want to test yours:

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 Demo on repl.it

Note that while printing the numbers there may be differences in 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 this output.

    
11.06.2014 / 17:53