基本语法
1、求100以内的所有素数
c = 0
for x in range(2,100):
for i in range(2,int( x ** 0.5)+1):
if x % i == 0:
break
else:
c += 1
print(x)
print(c)
2、求任意整数的几何级数
a = 3
q = 2
n = 10
sum = 0
for i in range(1,n+1):
sum += a * q ** i
print(sum)
3、求菲波那切数列的第101位
x = 0
y = 0
for i in range(0,102):
if i == 0:
y=1
elif i == 1:
x = 1
y = 1
else:
tmp = y
y = x + y
x = tmp
print(y)
转载于:https://blog.51cto.com/yanruohan/1898142