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

3
  • 1
    nz is an empty list. nz[i] is not valid for any i. Commented Apr 2, 2013 at 16:41
  • At what value of i does it error on you? Commented Apr 2, 2013 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. Commented Apr 2, 2013 at 18:00

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

5 Comments

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 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
stackoverflow.com/faq please! You can edit your comments, no need for "spamming" them!
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 ???
stackoverflow.com/questions/1751334/… read the pdf here then :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.