-1
def fact(n):
    fac = 1
    while (n>1):
        fac = fac*n
        n -= 1

    return fac

z = 0
t = int(raw_input())
nz = []
for i in range(0,t):
    c = 0
    n = int(raw_input())
    z = fact(n)

    z = list(str(z))
    for j in range(len(z)-1,1,-1):
        if z[j] != '0':
            break
        else:
            c +=1
    nz[i].append(c)
for k in range(0,t):
    print nz[k]

Hello I am getting

Indexerror : index out of range at " nz[i].append(c)

This program should calculate trailing zeros in the factorial of N. Can you also please help me optimize my code, so it can run also for large values of N?

Samuele Mattiuzzo
  • 10,760
  • 5
  • 39
  • 63
sonny
  • 9
  • 3
  • 1
    `nz` is an empty list. `nz[i]` is not valid for any `i`. – Wooble Apr 02 '13 at 16:41
  • At what value of `i` does it error on you? – John Apr 02 '13 at 16:43
  • If you want to solve this problem quickly, you have to avoid calculating factorial. With right method you should be able to calculate number of trailing zeros of `1000!` by hand in a minute. Then generalize it to algorithmic solution. – zch Apr 02 '13 at 18:00

2 Answers2

6

nz is empty list. It doesn't have any elements, so nz[i] would always raise IndexError. Perhaps you meant nz.append(c) ie. add c at the end of nz.

zch
  • 14,931
  • 2
  • 41
  • 49
0

This is how does append() work:

list.append(x)

Add an item to the end of the list; equivalent to a[len(a):] = [x].

so you may want to change nz[i].append(c) to nz.append(c), since your i index is already handled by the append function. You're actually assuming you have an i element in your list, which is false, since you are using an empty list

About optimizing, your problem is probably due to your recursion limit. Try import sys; sys.getrecursionlimit() in your python shell, you should see something like 1000 as result.

Swapping for an iterative version of the factorial function could be a start

def fact(n):
    r = 1
    for x in range (n):
        r = r * (x+1)
    return r
Community
  • 1
  • 1
Samuele Mattiuzzo
  • 10,760
  • 5
  • 39
  • 63
  • Also please state how to optimize the program so that it will give fast outputs for large inputs of the order of 10000000 – sonny Apr 02 '13 at 16:51
  • Also please state how to optimize the program so that it will give fast outputs for large inputs of the order of 10000000 ??? also please state i used previously a recursive function or factorial, but it was giving error (an error was given to a given line no. where the return statement was there) for larger inputs of the order of 10^8 – sonny Apr 02 '13 at 16:54
  • http://stackoverflow.com/faq please! You can edit your comments, no need for "spamming" them! – Samuele Mattiuzzo Apr 02 '13 at 17:10
  • No not an interactive way , but rather an efficient algorithmic way to calculate factorials for very large no.s of the order of 10 ^8 or 10^9 ??? – sonny Apr 02 '13 at 17:17
  • http://stackoverflow.com/questions/1751334/fast-algorithms-for-computing-the-factorial read the pdf here then :) – Samuele Mattiuzzo Apr 02 '13 at 17:20