Python:每日一题 106
本帖最后由 冬雪雪冬 于 2017-9-30 21:46 编辑首先我们的玩法做了一下改变:
1. 楼主不再提供答案。
2. 为了让大家独立思考,跟帖改为“回帖仅作者可见”。
3. 鼓励大家积极答题,奖励的期限为出题后24小时内。
4. 根据答案的质量给予1~3鱼币的奖励。
题目:
我们知道数字2、3既是质数又属于斐波那契数列,要求求出1000以内所有既是质数又属于斐波那契数列的数字。 import math
def fun(num):
if num == 1 :
return 1
elif num == 2:
return 1
else:
return fun(num-1) + fun(num - 2)
fun_num = []
n = 1
while True:
prime = True
# 获得斐波那契数
totle = fun(n)
if totle >= 1000:
break
else:
if totle != 1:
# 判断斐波那契数是不是质数
num =math.floor(math.sqrt(totle))
for j in range(2,num+1):
if totle % j == 0 :
prime = False
break
if prime:
fun_num.append(totle)
n += 1
print(fun_num) 本帖最后由 jerryxjr1220 于 2017-9-29 13:58 编辑
根据Sixpy大大的建议改进:
f1,f2=1,2
while f2<1000:
is_prime = lambda n: True if n in {2,3,5,7,11,13,17,19,23,29,31,37} else all(False for p in {2,3,5,7,11,13,17,19,23,29} if pow(p,n-1,n) != 1)
if is_prime(f2): print(f2, end=' ')
f1,f2 = f2,f1+f2
2 3 5 13 89 233 def fun(n):
a, b = 2, 3
while b < n:
if is_prime(a):
yield a
a, b = b, a+b
def is_prime(n):
for i in range(2,n):
if n % i == 0:
return False
return True
for i in fun(1000):
print(i) 取n以内质数集合,与100项以内斐波那契集合取交集
第100项斐波那契已经有10的20次方了,基本保证 >n
def fun(n):
prime = {i for i in range(2,n+1) if 0 not in (i%j for j in range(2,int(i**0.5)+1))}
fib = {k for k in [(a[-1],a.append(a[-1]+a[-2])) for a in (,) for j in range(100)]}
return prime & fib 感觉自己的代码写的可丑……
#求出斐波拉契数列
m = 0
n = 1
total = 1
list1 = []
while True:
total = m + n
if total < 1000:
list1.append(total)
m = n
n = total
else:
break
#求出质数
list2 =
for each in list1:
t = 0
each = int(each)
for i in range(2,each):
if (each % i) != 0:
t = 1
else:
t = 0
break
if t != 0:
list2.append(each)
print(list2)
输入结果: 递归写法#求出斐波拉契数列
def fun(n):
if n <= 1 :
return n
else:
result = int(fun(n-1))+ int(fun(n-2))
returnresult
list1 = []
for i in range(17):
list1.append(fun(i))
#求出质数
list2 =
for each in list1:
t = 0
each = int(each)
for i in range(2,each//2 + 2):
if (each % i) != 0:
t = 1
else:
t = 0
break
if t != 0:
list2.append(each)
print(list2) import math
def double_check(x):
def fibonacci(n):
if n == 0:
return 0
if n == 1:
return 1
return fibonacci(n-1) + fibonacci(n-2)
def prime_check(y):
list_a =
list_prime = []
for m in list_a:
for k in range(2,m):
if m%k == 0 and k != 1:
break
else:
list_prime.append(m)
for l in list_prime:
if y%l == 0:
return False
return True
n = 3
result_list = []
while fibonacci(n) <= x:
if prime_check(fibonacci(n)):
result_list.append(fibonacci(n))
n += 1
return result_list
if __name__ == '__main__': def isprime(n):
if n == 1:
return False
if n == 2 :
return True
for i in range(2,n//2 + 1):
if n % i == 0:
return False
return True
def fab():
n1, n2 = 1, 1
while True:
n1, n2 = n2,n1+n2
yield n1
a = fab()
while True:
b = next(a)
if b <= 1000 and isprime(b):
print(b) x=1
y=1
z=1
m=0
q=[]
while m<=20:
z=x+y
x=y
y=z
q.append(z)
m+=1
p=[]
for a in range(2,101):
if a==2:
p.append(a)
else:
if 0 not in :
p.append(a)
for h in q:
if h in p:
print(h)
solomonxian 发表于 2017-9-29 21:20
取n以内质数集合,与100项以内斐波那契集合取交集
第100项斐波那契已经有10的20次方了,基本保证 >n
怎么您对python的语法,各种库函数这么熟悉啊, list1 = []
def addNum(num) :
if num in :
return 1
else :
return addNum(num - 1) + addNum(num - 2)
def getNum() :
globallist1
for i in range(20) :
if addNum(i) <= 1000 :
list1.append(addNum(i))
else :
break
print(list1)
def getPrime() :
getNum()
global list1
list2 = []
for i in list1 :
flag = 0
for j in range(2, i // 2 + 1) :
if i == 1 :
continue
if i % j == 0 :
flag += 1
if flag == 0 :
list2.append(i)
for i in list2[:] :
if i == 1 :
list2.remove(i)
print(list2)
getPrime() def fib(num) :
if num in :
return 1
else :
return fib(num - 1) + fib(num - 2)
def isPrime(num) :
flag = 0
for i in range(2, num // 2) :
if num % i == 0 :
flag += 1
if flag > 0 :
return False
else :
return True
def getNum() :
list1 = []
for i in range(1, 20) :
if fib(i) >= 1000 :
break
else :
list1.append(fib(i))
for i in list1[:] :
if (i == 1) or (not isPrime(i)) :
list1.remove(i)
print(list1)
getNum() def fun106():
a = 1
b = 1
list1 = []
while b < 1000:
a, b = b, a+b
prime = True
if b > 3:
for i in range(2,b):
#排除非质数
if b%i == 0:
prime = False
break
if prime:
list1.append(b)
return list1[:-1]
print(fun106()) #判断费那波切数:
def fei():
s=
a=1
b=1
for i in range(1000):
a,b=b,a+b
if a>=1000:
break
s.append(a)
return s
#判断是否为质数
def zhi():
z=[]
for i in range(2,1000):
for j in range(2,i):
if i%j==0:
break
else:
z.append(i)
return z
a=zhi()
b=fei()
for i in a:
for j in b:
if i==j:
print(i)
页:
[1]