[Python Python] Examples of methods find the original code number daffodils

Numbers by daffodils in Python programming method, a line of python code will solve the problem.

Must first know what is the number of daffodils, then go write code, this can be very effective.

It refers to a number of n-bit daffodils positive integer (n≥3), on which digital bits of each n-th power sum is equal to itself.

The method of seeking original Python code number daffodils following examples:

#method1
print [x for x in range(10**5) if sum(i**len(str(x)) for i in [int(j) for j in str(x)])==x]
 
#method2
print filter(lambda n: sum(map(lambda x: x**len(str(n)),map(int,list(str(n)))))==n,range(10**5))
 
#method3
from itertools import product
print [[n*'%s'%(i) for i in product(*([range(1,10)]+[range(0,10)]*(n-1))) if reduce(lambda x,y: 10*x+y,i)==sum(k**n for k in i)] for n in range(1,5+1)]

Recommended reading:

Basics zero Python's most detailed source of tutorials

2019 Python Reptile Learning Roadmap full version

Why Python can be firmly secured the first card AI Artificial Intelligence Language

Python rise, TIOBE list of programming languages ​​a new high!

Guess you like

Origin blog.csdn.net/kkk123789/article/details/93618071