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

×

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

asked 05 Sep '13, 12:55

bax638's gravatar image

0★bax638
1111
accept rate: 0%

edited 05 Sep '13, 14:56

kcahdog's gravatar image

3★kcahdog
10.0k2854129


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.

link

answered 05 Sep '13, 15:10

kcahdog's gravatar image

3★kcahdog
10.0k2854129
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 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

(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:
        p+=1
        ans += m // 5**p
    print(ans)

Hope this helps... :)

link

answered 26 Aug '18, 22:39

ankush_953's gravatar image

4★ankush_953
657
accept rate: 8%

toggle preview
Preview

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "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:

×529
×525
×435
×231

question asked: 05 Sep '13, 12:55

question was seen: 1,669 times

last updated: 05 Sep '13, 18:40