You are not logged in. Please login at to post your questions!


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


asked 05 Sep '13, 12:55

bax638's gravatar image

accept rate: 0%

edited 05 Sep '13, 14:56

kcahdog's gravatar image


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.

4) 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.


answered 05 Sep '13, 15:10

kcahdog's gravatar image

accept rate: 14%

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

(05 Sep '13, 15:12) kcahdog3★

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).

(05 Sep '13, 16:28) betlista ♦♦3★

@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?

(05 Sep '13, 18:25) kcahdog3★

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

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

(05 Sep '13, 18:29) betlista ♦♦3★

Any idea if it can be done in Python?

(05 Sep '13, 18:35) kcahdog3★

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...

(05 Sep '13, 18:37) betlista ♦♦3★
showing 5 of 6 show all
test = int(input())
for i in range(test):
    n = int(input())
    m = n
    p = 0
    ans = 0
    while n > 5**p:
        ans += m // 5**p

Hope this helps... :)


answered 26 Aug '18, 22:39

ankush_953's gravatar image

accept rate: 8%

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 05 Sep '13, 12:55

question was seen: 1,669 times

last updated: 05 Sep '13, 18:40