怎么把这个reduce写的代码改成for来写
这段代码是求素数的,reduce看起来吃力,要按照同样的算法来做
谢谢各位大神
reduce(lambda i,n: i if 0 in [n%x for x in i] else i+[n] , xrange(2,100), [])
F = lambda i, n: i if 0 in [n % x for x in i] else i + [n]
R = []
for i in range(2, 100):
R = F(R, i)
print R
def Fi(i, n):
res = [n % x for x in i]
if 0 in res:
return i
else:
return i + [n]
R = []
for i in range(2, 100):
R = Fi(R, i)
print R
def F(l, n):
for x in l:
if n % x == 0:
return
l.append(n)
def P(l, n):
from math import sqrt
m = sqrt(n)
for x in l:
if (x <= m) and (n % x == 0):
return
l.append(n)
R = []
for i in range(2, 100):
P(R, i)
print R
def P(l, n):
from math import sqrt
m = sqrt(n)
for x in l:
if x <= m:
if n % x == 0:
return
else:
break
l.append(n)
R = []
for i in range(2, 1000000):
P(R, i)
print R
q = list(range(2, 10000))
l = []
while 1:
if len(q) <= 2:
l.extend(q)
break
else:
qs = q.pop(0)
l.append(qs)
qm = q[len(q) - 1]
q1 = set(qs * n for n in range(2, int(qm / qs) + 1))
q = list(set(q) - q1)
q.sort()
print(l)
from math import sqrt
from datetime import datetime, timedelta
def P(l, n):
m = sqrt(n)
for x in l:
if x > m:
break
if n % x == 0:
return
l.append(n)
def Run(F, M):
R = []
tStart = datetime.now()
for i in range(2, M):
F(R, i)
tEnd = datetime.now()
tDelta = tEnd - tStart
print tDelta
L = len(R)
print L, R[0 : L if L < 10 else 10]
M = 1000000
Run(P, M)
n = 1000000
a = [True] * (n + 1)
b = []
for i in range(2, int(n ** 0.5) + 1):
if a[i]:
for j in range(i, int(n / i) + 1):
a[j * i] = False
for i in range(2, n + 1):
if a[i]:
b.append(i)
print(b)
a = [True] * (n + 1)
b = [2]
for i in range(3, int(n ** 0.5) + 1, 2):
if a[i]:
for j in range(i*i, n + 1, i):
a[j] = False
for i in range(3, n + 1, 2):
if a[i]:
b.append(i)