Of course We needn't 100 levels,but I use the exec function can concise the code. See the attachment.
2008/8/17 Fredrik Lundh <[EMAIL PROTECTED]> > Nick Dumas wrote: > > A good quote I read (I can't remember who it was from, though) is "If >> you need more than three levels of indentation, then something is >> seriously wrong with your code." Possibly Guido himself? Anyway. If >> you've got 100 levels of for, you're probably making things way harder >> than they need to be. >> > > assuming 100 levels of for and 2 items in each sequence, you'll end up with > 1267650600228229401496703205376 iterations through the inner loop. assuming > each iteration takes a picosecond, it'll take approx 40 billion years to run > the program. > > </F> > > > -- > http://mail.python.org/mailman/listinfo/python-list >
#========================================================================= # # Python Source File -- Created with SAPIEN Technologies PrimalScript 4.1 # # NAME: Problem 52 # # AUTHOR: patrol , ustc # DATE : 2008-8-7 # # COMMENT: # # URL:http://projecteuler.net/index.php?section=problems&id=52 # Result:142857 #========================================================================= import sys import string def isPro52Num(n): s=[] for i in range(1,7): s.append(str(i*n)) for i in s[0]: ll = len(string.join(string.split(s[0],i),'')) for j in range(1,6): if ll!=len(string.join(string.split(s[j],i),'')): return False return True def f(n): index=[] num=[] digitsList=[0]+range(2,10) for i in range(n): index.append(1) num.append(1) num.append(1) stringCode='for index[0] in digitsList:\n' for i in range(1,n): stringCode += '\t'*(i+0)+'num[%d] = num[%d]*10 + index[%d]\n'%(i-1,i-2,i-1) \ +'\t'*(i+0)+'for index[%d] in digitsList:\n'%i stringCode +='\t'*n+'num[%d] = num[%d]*10 + index[%d]\n'%(n-1,n-2,n-1) \ +'\t'*n+'if isPro52Num(num[%d]):\n'%(n-1) \ +'\t'*(n+1)+'print num[%d]\n'%(n-1) \ +'\t'*(n+1)+'sys.exit()\n' exec(stringCode) length = 1 while True: f(length) length += 1
-- http://mail.python.org/mailman/listinfo/python-list