Runtime Error(NZEC) Factorial Trailing Zeros...

#!/usr/local/bin/python2.7
import sys
num_list = []
try:
n = int(raw_input())
except EOFError:
sys.exit(0)
for i in range(n):
try:
input_no = int(raw_input())
except EOFError:
sys.exit(0)
num_list.append(input_no)
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)
zero_list.append(count)
print count
fact_zero()

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.

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 http://www.codechef.com/viewsolution/2106885

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 http://www.codechef.com/viewsolution/2158899

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…

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

Hope this helps… :slight_smile: