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(" ")