读程序,总结程序的功能:
1.
numbers=1
for i in range(0,20):
numbers*=2
print(numbers)**
功能:输出2的20次方
2.
summation=0
num=1
while num<=100:
if (num%3==0 or num%7==0) and num%21!=0:
summation += 1
num+=1
print(summation)
功能:统计小于100的数字中能被3或者能被7整除,但是不能被21整除的数的个数。
3. 求1到100之间所有数的和、平均值
num1 = 0
for x in range(1, 101):
num1 += x
print('和:', num1, '平均值', num1 / 100)
num1 = 0
y = 1
while y <= 100:
num1 += y
y += 1
print('和:', num1, '平均值', num1 / 100)
结果:
和: 5050 平均值 50.5
和: 5050 平均值 50.5
Process finished with exit code 0
4.计算1-100之间能被3整除的数的和
num2 = 0
for x in range(1, 101):
if x % 3 == 0:
num2 += x
print(num2)
num2 = 0
y = 1
while y <= 100:
if y % 3 == 0:
num2 += y
y += 1
print(num2)
结果:
1683
1683
Process finished with exit code 0
5.计算1-100之间不能被7整除的数的和
num3 = 0
for x in range(1, 101):
if x % 7 != 0:
num3 += x
print(num3)
num3 = 0
y = 1
while y <= 100:
if y % 7 != 0:
num3 += y
y += 1
print(num3)
结果:
4315
4315
Process finished with exit code 0
稍微困难
1.求斐波那契数列中第n个数的值:1,1,2,3,5,8,13,21,34....
n = int(input('请输入一个正整数'))
a = 1
b = 1
c = 1
for x in range(3, n + 1):
c = a + b
a = b
b = c
print(n, '值', c)
2.判断101-200之间有多少个素数,并输出所有素数。判断素数的方法:用⼀一个数分别除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数.
for x in range(101, 201):
for y in range(2, x):
if x % y == 0:
break
else:
print('素数是', x)