Runtime Error(NZEC) Factorial Trailing Zeros...

import sys
num_list = []
n = int(raw_input())
except EOFError:
for i in range(n):
input_no = int(raw_input())
except EOFError:
zero_list = []
def fact_zero():
# for num in num_list:
count = 0
for n in range(1,num+1):
denom = 5
power = 1
while n >= denom:
denom = 5**power
if n % denom == 0:
count = count + 1
power = power + 1
str_count = str(count)
print count

1)The NZEC is being caused by this line: "for n in range(1,num+1): ". here num is not defined in your code.

2)There is no need of try catch blocks as input is already in valid format.

3)Ther is no need to input all, calculate for all and then output them. best is to input one number, solve for it and then outputthe answer and proceed to next test case.

  1. I think you meant to uncomment this: “# for num in num_list:”. also your logic is O(n) for each test case. it will exceed time limit. come up with a better solution.

test = int(input())
for i in range(test):
n = int(input())
m = n
p = 0
ans = 0
while n > 5p:
ans += m // 5

Hope this helps… :slight_smile:

PS. print count will print the whole list and not individual numbers. you might want to try : for i in zero_list: print i

addition for 3: sometimes it’s better to print result to buffer and then print that buffer at the end, specially for languages with slow I/O like Java (flushing output after each test case can be slow).

@betlista do you mean storing it an array and then printing at end or storing it directly in the buffer? could you please elaborate on how it is done?

No storing in array and then print items one by one, that won’t solve then problem of slow I/O. In Java I’m using StringBuilder to create whole result and then printing once that big String.

Here is an example CodeChef: Practical coding for everyone

In problem there can be 10^5 outputs, so instead of printing one by one I print one big String at the end of program.

Another example CodeChef: Practical coding for everyone

Any idea if it can be done in Python?

It’s simple string concatenation, so I guess it can be done, just one need to take care about formats, for example printing doubles with 6 decimal places…