#! /usr/bin/python # -*- coding: iso-8859-1 -*- """ Problem from: http://www.umassd.edu/mathcontest/9999.cfm Determine the ones digits for the following expression: 0! + 1! + 2! + 3! + 4! + . . . + 9998! + 9999! Result: python factorialOnes.py 9999 Numbers of digits '1' in this factorial expression: 4000 Author: Cesar Kallas - cesarkallas at gmx . net http://cesarkallas.soulivre.org Date: 28/10/2006 """ import sys def factorial(num): f = 1 ones = 0 while (num > 0): f = f * num ones = ones + str(num).count("1") #print num, " " num = num - 1 return ones, f if __name__ == "__main__": if len(sys.argv) < 2: print "Usage: python %s factorial" % sys.argv[0] sys.exit() else: ones, fat = factorial(int(sys.argv[1])) print "Fatorial of %s is: " % sys.argv[1], fat print "Numbers of digits '1' in this factorial expression: ", ones