无意中看到介绍水仙花数,即一个3位数的各位数字的3次方和等该数本身
实际上就是自幂数的一种,感觉有点意思,写个脚本试了下
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys, time
def deco(func):
def inner(*args, **kwargs):
b_time = time.time()
rlt = func(*args, **kwargs)
e_time = time.time()
print 'elapse:', e_time - b_time
return rlt
return inner
@deco
def daffodilNumber(bit):
rlt = []
start, end = pow(10, bit - 1), pow(10, bit)
for i in range(start, end):
if isDaffodilNumber(i):
rlt.append(i)
return rlt
def isDaffodilNumber(number):
tmp, rlt = 0, False
strNum = str(number)
for i in strNum:
tmp += pow(int(i), len(strNum))
if tmp == number:
rlt = True
return rlt
if '__main__' == __name__:
bit, lp = 0, []
if len(sys.argv) > 1:
bit = int(sys.argv[1])