0
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(" ")
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. :)
– Luiz Vieira