Python method for solving the number of daffodils

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.