Factorial series from thehuxley.com site

Asked

Viewed 471 times

9

Make a program that calculates and writes the sum of the n first series terms:

seriefatorial http://www.thehuxley.com/data/images/app/problems/temp/e68085c6d699d2c7029907f994c57b80.png

Input format An integer n indicating the number of terms in the series

Output format A floating point number formatted to two decimal places, followed by a line end.

Input example: 5 Exit: 3.46

This question is from thehuxley.com, when submitting the question of this error: "The result of the evaluation was 'WRONG_ANSWER' which means that your program did not return the expected answer."

Only I’ve done several tests with higher values and the result is expected... I have no idea what the test case is that is generating the wrong value. Does anyone know?

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

n = int(input())
count, value = 0, 0

for number in range(1, n + 1):
   count += count + 1   

   if(number % 2 == 0):  # par
       value -= fatorial(number) / count
   else:
       value += fatorial(number) / count

print("%.2f" % value)
  • It is not the lack of the end-of-line character?

2 answers

8

Your problem is that both the return of fatorial as to the count are integers. In Python 2, when an integer is divided by another, the result is always an integer:

>>> 3/2
1
>>> 3.0/2
1.5
>>> 3/2.0
1.5

While in Python 3 the result is a float. Your code - in particular the use of parentheses after the print - suggests that you are using 3, but nothing prevents the site from using 2.

To solve, just change the function fatorial to return the result in floating point:

def fatorial(number):
    if number == 0:
       return 1.0
    ...

And - if necessary - put a line break at the end (such as suggested in the comments):

print("%.2f\n" % value)
  • The compilation version of the site is 3.2. Even so I tested with the changes you suggested and nothing.

  • @Fsdeveloper I’ll let someone who has an account on this site answer, because I’m out of ideas (it seems all right). My last suggestion is to try to round up down instead of "nearest pro value" (i.e. do not use standard rounding): change the last line to print("%.2f" % (math.floor(value*100)/100)) and see to it... with and without the \n at the end (by guarantee)

  • It did not work. But obeyed by the help. When I discover I put here.

2


These issues where you accumulate the result of multiple divisions between floating points can accumulate small accuracy errors until a moment where it becomes significant. Thus, depending on how the language treats the floating point, the answer may vary somewhat between them. I emailed the people on the site and they reviewed the test cases. They said that there were actually some cases that depending on the combination of language C, Pascal, Java, Python and the type used to treat the floating point (float, double, long double) the result could give slightly different. They said they changed the test cases to avoid these situations. After that, I took the liberty of taking your solution and submitting to the site and the result was CORRECT.

  • Problem solved. Thank you r0drigopaes

Browser other questions tagged

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