1.素数,求1-100之间的素数(想在代码中写中文注释,加一句#coding=utf-8,注意等号左右没空格)
def isPrime(n):
if(n == 1):
return False
else:
for i in range(2,n):
if(n % i == 0):
return False
return True
print filter(isPrime,range(1,101))
2.斐波那契数列实现
2.1 构造
a, b = 0, 1
while b < 40:
print b
b=a+b
a=b-a
2.2 斐波契纳数列1,2,3,5,8,13,21............根据这样的规律,求出400万以内最大的斐波契纳数
def fibs2(num):
x = [1,1]
while x[-1] < num:
x.append(x[-2]+x[-1])
if x[-1] < num:
return x[-1]
else:
return x[-2]
print fibs2(4000000)
fib = lambda x,y:y if x+y>4000000 else fib(y,x+y)
print fib(0,1)
print fib(0,1)