Python:每日一题 207
本帖最后由 冬雪雪冬 于 2018-9-12 19:45 编辑我们的玩法做了一下改变:
1. 楼主不再提供答案。
2. 请大家先独立思考,再参考其他鱼油的解答,这样才有助于自己编程水平的提高。开始阶段是看不到其他人的回帖的,等答题完成,开始评分时再取消限制。
3. 鼓励大家积极答题,奖励的期限为出题后24小时内。
4. 根据答案的质量给予1~3鱼币的奖励。
题目:
本题与206题类似,斐波那契数中有些数字是可以分别被其每一位数字整除的(忽略数字0),例如:
46368可以被3,4,6,8整除。
主要看大家的程序效率,比如能被6整数就不用考虑2和3了。
要求求出前10个这样的斐波那契数(不包括一位数1, 2, 3, 5, 8)。
**** Hidden Message ***** 本帖最后由 凌九霄 于 2018-9-11 15:17 编辑
import timeit
P = '''
def fibonacci(n):
if n > 0:
i, a, b = 0, 0, 1
while i < n:
a, b = b, a + b
i += 1
return a
def fnum(n):
t = set(str(n)) - {'0', '1'}
if '6' in t:
t = t - {'2', '3'}
if '8' in t:
t = t - {'2', '4'}
return list(map(int, t))
i, j = 7, 0
while j < 10:
f = fibonacci(i)
t = fnum(f)
for k in t:
if f % k:
break
elif k == t[-1]:
print(f)
j += 1
i += 1
'''
print('运行用时:',timeit.timeit(stmt=P, number=1))
本帖最后由 拉了盏灯 于 2018-9-12 00:00 编辑
def feb(n):
a,b = 0,1
while b<n:
a,b=b,a+b
if b > 9:
l=list(map(int,str(b)))
if len(l)-l.count(0) == len():
yield b
for i in feb(10**200):
print(i,'\n')
本帖最后由 塔利班 于 2018-9-12 10:37 编辑
感觉找去因数的方法很蠢,回头再写- -
def check(x):
p=sorted()
q=p[:]
l=len(p)
for i in range(l):
for j in range(i+1,l):
if q%q==0:
try:
p.remove(q)
except:
pass
for each in p:
if x%each:
return False
return True
def fib():
a,b=8,13
while True:
a,b=b,a+b
if check(a):
yield a
a=fib()
for i in range(10):
print(next(a))
这个没筛,有时候感觉处理了也没提升运行速度,应该有更好的算法和内置吧
from functools import reduce
from math import gcd
a,b,c=8,13,0
while c<10:
a,b=b,a+b
if not a%reduce(lambda x,y:x*y//gcd(x,y),,1):
c+=1
print(a) 本帖最后由 graceasyi 于 2018-9-11 14:20 编辑
def fib_itr(n):
if n < 0:
return None
if n <= 1:
return (0, 1)
a, b = 0, 1
for i in range(n-1):
a, b = b, a+b
return b
i = 7
n = 0
while n < 10:
fbi = fib_itr(i)
sm = set()
if 0 in sm: sm.remove(0)
if 1 in sm: sm.remove(1)
if 9 in sm:
if 3 in sm: sm.remove(3)
if 8 in sm:
if 2 in sm: sm.remove(2)
if 4 in sm: sm.remove(4)
if 6 in sm:
if 2 in sm: sm.remove(2)
if 3 in sm: sm.remove(3)
if 4 in sm:
if 2 in sm: sm.remove(2)
qt =
if qt.count(0) == len(qt):
print(fbi)
n += 1
i += 1
结果:
55
144
46368
5358359254990966640871840
64202014863723094126901777428873111802307548623680
769246427201094785080787978422393713094534885688979999504447628313150135520
9216845717656874712980450562726202415567360565980794777111390850331644813674856981646960226192287360
110433070572952242346432246767718285942590237357555606380008891875277701705731473925618404421867819924194229142447517901959200
1323171012053243520828784042795469593341319770463238313551473338336502410952765153371119398122747569819754164672344667591018783803781288766524146031040
15853779289614481577191691577533518174485949824765525957623037737532160833631593681512535189759649213804709691705593604227037581022758330489263385511067756715301085561591382880
k {:5_110:} def fib():
a,b = 0,1
while True:
a,b = b,a+b
yield a
def my_filter(num):
each_num = []
org_num = num
while num:
temp = num%10
if temp not in each_num:
if temp != 0 and temp != 1:
each_num.append(temp)
num //= 10
#如果有9不考虑3;如果有8不考虑4和2;如果有6不考虑3和2;如果有4不考虑2。
if 9 in each_num:
if 3 in each_num:
each_num.remove(3)
if 8 in each_num:
if 4 in each_num:
each_num.remove(4)
if 2 in each_num:
each_num.remove(2)
if 6 in each_num:
if 3 in each_num:
each_num.remove(3)
if 2 in each_num:
each_num.remove(2)
if 4 in each_num:
if 2 in each_num:
each_num.remove(2)
for each in each_num:
if org_num%each:
return False
return True
def fun207():
count = 0
for each_fib in fib():
if each_fib > 10 and my_filter(each_fib):
print(each_fib)
count += 1
if count == 10:
break
if __name__ == '__main__':
fun207() 本帖最后由 骨傲天 于 2018-9-11 16:17 编辑
def b():
a=1
b=0
while True:
yield a+b
a,b=a+b,a
x=1
for i in b():
if i<10:
continue
elif x<11:
r=list(map(int,str(i)))
re=[]
d,s=0,0
for y in r:
if y in re:
continue
if y!=0 and i%y:
break
elif y%2 and not s:
re.extend()
s=1
elif y%3 and not d:
re.extend()
d=1
else:
re.append(y)
else:
print(i)
x+=1
else:
break 本帖最后由 老猫NAKE 于 2018-9-11 15:54 编辑
走错片场了。。 本帖最后由 天圆突破 于 2018-9-11 22:22 编辑
class Feb:
def __init__(self):
self.a = self.b = 1
def __iter__(self):
return self
@staticmethod
def filter_num(n):
# 取集合,筛去0和1
lst = list(filter(lambda x:x!=0 and x!=1,set(map(lambda x:int(x), str(n)))))
# 倒序排序,早碰上早滚蛋,提高效率
lst.sort(reverse=True)
for each_n in lst:
if n%each_n != 0:
return False
return True
def __next__(self):
while True:
self.a, self.b = self.b, self.a+self.b
if self.filter_num(self.a) and self.a > 10:
return self.a
if __name__ == '__main__':
for i in Feb():
input(i)
我觉得考虑效率的话,去重去01外加逆排序就足够了
如果为了去掉2,3,4(只能去这3个数字),需要遍历4次才能去干净(从集合里找9864),不去的话只会多遍历3次(带着234去除).
所以还是不去了吧. 本帖最后由 胡尔汉 于 2018-9-11 19:40 编辑
实在没有好的办法按照题目的方法优化效率,能不能给点建议或其他思路?
def confirm_num(num):
l1 =
num1 = str(num)
for i in num1:
i = int(i)
if i in l1:
if num%i != 0:
return False
else:
l1.remove(i)
return True
x,y,i = 5,8,0
list1 = []#存放符合要求的数字
while i <10:
x,y = y,x+y
if confirm_num(y):
list1.append(y)
i+=1
print(list1) 学习有限,目前也就仅到能做出来的程度了,后续改进{:10_266:}
import copy
i = 1
n=0
num1 = 5
num2 = 8
while n < 10:
num3 = num2 + num1
#print ('num3=',num3) #斐波那契数
x=copy.copy(num3) #
s=len(str(x))
h=0
sum=0
sz=[]
while h<s:
sz = sz +
x=x-sz*(10**(s-h-1))
if sz != 0:
e = num3 % sz
sum+=e
h+=1
''' h=h+1
sz.sort()
rz=sz
m=0
while m<len(rz)-1:
if rz==rz:
rz.remove(rz)
else:
m=m+1
p=0
while p<len(rz)-1:
if rz != 0:
e = num3 % rz
sum+=e
p+=1
'''
if sum==0:
print (num3)
n=n+1
print('n=',n)
i += 1
num1 = num2
num2 = num3
答案:
55
n= 1
144
n= 2
46368
n= 3
5358359254990966640871840
n= 4
64202014863723094126901777428873111802307548623680
n= 5
769246427201094785080787978422393713094534885688979999504447628313150135520
n= 6
9216845717656874712980450562726202415567360565980794777111390850331644813674856981646960226192287360
n= 7
110433070572952242346432246767718285942590237357555606380008891875277701705731473925618404421867819924194229142447517901959200
n= 8
1323171012053243520828784042795469593341319770463238313551473338336502410952765153371119398122747569819754164672344667591018783803781288766524146031040
n= 9
15853779289614481577191691577533518174485949824765525957623037737532160833631593681512535189759649213804709691705593604227037581022758330489263385511067756715301085561591382880
n= 10 {:10_277:} def feibonacci(num):
a,b = 0,1
count = 0
while count <num:
yield a
a,b = b,a+b
count +=1
def fun207(n):
result = list()
for i in feibonacci(5000):
if i in :
pass
else:
for each in str(i):
if int(each) == 0:
pass
elif i % int(each) == 0:
a=True
else:
a=False
break
if a:
result.append(i)
if len(result)==n:
break
print(result)
if __name__ == '__main__':
fun207(10) {:5_109:} 本帖最后由 谁与争锋 于 2018-9-11 21:26 编辑
list1 = []
list2 =
while len(list1)<10:
list2[-2],list2[-1]=list2[-1],list2[-1]+list2[-2]
a=list2[-1]
#print(a)
num1=1
b=str(a)
c=list(b)
c=list(set(c))
c.sort(reverse=True)
#print('c=',c)
list3 = []
if '0' in c:
c.remove('0')
while len(c)>0:
d=int(c.pop(0))
list3.append(d)
if num1%d!=0:
num1*=d
list4 = c.copy()
for e in list4:
if d%(int(e))==0:
c.remove(e)
if a%num1==0:
list1.append(a)
print(list1)
本帖最后由 archlzy 于 2018-9-11 22:34 编辑
def f207(n):
def creat_num(m):
a, b = 1, 0
for i in range(m):
a ,b = a+b ,a
return a
result = []
m = 0
while len(result) < n:
m += 1
temp = creat_num(m)
if temp >10:
temp_set = set(])
temp_set -= set()
temp_a = 0
for i in temp_set:
temp_a += temp % int(i)
if temp_a == 0:
result.append(temp)
return result
没抢到 206的楼 好遗憾~
def fun207(n):
def fib():
a, b = 0, 1
while 1:
a, b = b, a + b
yield a
g = fib()
flt = filter(lambda x: len(str(x)) > 1 and not sum(map(lambda y, x=x: x % y, {int(i) for i in str(x)} - {0, 1})), g)
return
print(fun207(10)) hao