I
am using both the THINK PYTHON text and the Challenge-You website to
learn Python. I am doing reasonably well and certainly enjoy the
available challenges.
I am currently attempting to work a challenge known as the 'Zeros of a Factorial' challenge located at http://www.challenge-you.com/challenge?id=484. The actual challenge is as follows: "It can easily be seen that 6! = 720 and has exactly one trailing zero. What is the lowest integer, x, such that x! has 7^20 trailing zeros?" It does not, on the surface, appear to be a frontal lobe breaker. Design an algorithm to build factorials; count the number of trailing zeros, and the first time we hit it, we have the lowest integer. To test, I computed factorials for 50,000--12,499 trailing zeros,100,000 trailing zeros--24,999, 150,000 trailing zeros 37,498, and finally 200,000 trailing zeros of 49,998. Obviously, running a test on 1000000 would take some time and would not even be close to the required number of trailing zeros. I need to know if I am even close with my approach to a workable algorithm and whatever suggestions you might have to speed up the process so that it would not take hours until it found the correct integer. Any and all suggestions as well as more efficient ways to code the algorithm will be most appreciated. Please see attached source code. Robert Berman |
#!/usr/bin/env python #It can easily be seen that 6! = 720 and has exactly one trailing zero. #What is the lowest integer, x, such that x! has 7^20 trailing zeros
def Computefact(value): a=value-1 j=value while a > 1: j=j*a a=a-1 return j def Getmaxzeros(v): str1=str(v) revstr=str1[::-1] knt=0 for chars in revstr: if chars=='0': knt+=1 else: break return knt def main(): trailzeroes=7**20 runit=True while runit==True: val_in=int(raw_input('Enter the number for which to find prime value, or 0 to quit ')) if val_in==0: runit=False else: x=Computefact(val_in) nbrzeros=Getmaxzeros(x) print 'nbr of zeroes: ', nbrzeros if nbrzeros>=trailzeroes: print x nbrzeros=Getmaxzeros(x) print 'nbr of zeroes: ', nbrzeros if nbrzeros>=trailzeroes: if __name__ == '__main__': main()
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor