In this paper, the method of solving Narcissus number by Python is described. Share to everyone for your reference. Specific as follows:
A n-bit decimal positive integer, if its number on each bit is n-th and equals the number itself, it is called the number of flowers.
#!/usr/bin/pythondef get_flower (n, ofile): D_pow=[pow (i,n) for I in Range (0,10)] V_min=1*pow (10,n-1) v_max=sum ((9*pow ( 10,X) for X in range (0,n)) t_count=0 print D_pow, V_max, V_min nums=[1]+[0]* (n-1) print ' Start: ', Nums idx=n-1 tmp_l=[0]* Ten while True: nums[idx]+=1 if nums[idx]<10: j=idx+1 while J
<="V_max" and="">
=v_min: T_count+=1 #test If is flower #print ' does test: ', '. Join (Map (str,nums)) k=0 while k<10: Tmp_l[k]=0 k+=1 n=0 for K in nums: tmp_l[k]+=1 n+=1 while n>0: p=v%10 If tmp_l[p]>0: tmp_l[p]-=1 n-=1 else: break v/=10 if n==0: print >> Ofile, ' hit ', Sum ((D_pow[x] for x in nums)) idx=n-1 elif idx==0: print ' Do ' break else: idx-=1 print ' T_count ', t_countif __name__ = = ' __main__ ': With file ('./f.txt ', ' WB ') as O: get_flower (+, O)
#get_flower (3, O)
Hopefully this article will help you with Python programming.