基础
读程序,总结程序的功能:
numbers=1
for i in range(0,20):
numbers*=2
print(numbers)
print(1*2**20)
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)
print('统计1-100之间能被3或7整除但不能被21整除的数的个数')
1. 求1到100之间所有数的和、平均值
sum1 = 0
for i in range(1,101):
sum1 += i
print('和是:',sum1,'平均值是:',sum1/100)
sum1 = 0
count = 1
while count <= 100:
sum1 += count
count += 1
print('和是:',sum1,'平均值是:',sum1/100)
2. 计算1-100之间能3整除的数的和
sum1 = 0
for i in range(1,101):
if i % 3 == 0:
sum1 += i
print('能被3整除的数的和是:',sum1)
sum1 = 0
count = 1
while count <= 100:
if count % 3 == 0:
sum1 += count
count += 1
print('能被3整除的数的和是:',sum1)
3. 计算1-100之间不能被7整除的数的和
sum1 = 0
for i in range(1,101):
if i % 7 != 0:
sum1 += i
print('不能被7整除的数的和是:',sum1)
sum1 = 0
count = 1
while count <= 100:
if count % 7 != 0:
sum1 += count
count += 1
print('不能被7整除的数的和是:',sum1)
进阶
1. 求斐波那契数列中第n个数的值:1,1,2,3,5,8,13,21,34....
num = int(input('输入数列的位置(正整数):'))
if num == 1 or num ==2:
value = 1
else:
value1 = 1
value2 = 1
value = 0
for i in range(1,num-1):
value = value1 + value2
value1 = value2
value2 = value
print('第',num,'位数的值是:',value)
2. 判断101-200之间有多少个素数,并输出所有素数。判断素数的方法:用一个数分别除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数
count = 0
for i in range(101,201):
for j in range(2,i):
if i % j == 0:
break
else:
print(i,'是素数')
count += 1
print('一共',count,'个素数')
3. 打印出所有的水仙花数,所谓水仙花数是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个水仙花数,因为153 = 1^3 + 5^3 + 3^3
for i in range(100,1000):
if i == (i // 100)**3 + (i // 10 % 10)**3 + (i % 10)**3:
print(i)
4. 有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的第20个分数
分子:上一个分数的分子加分母 分母: 上一个分数的分子 fz = 2 fm = 1 fz+fm / fz
a1 = 1
a2 = 2
for i in range(1,20):
a = a1 + a2
a1 = a2
a2 = a
print(a2,'/',a1)
5. 给一个正整数,要求:1、求它是几位数 2.逆序打印出各位数字
num = int(input('请输入一个正整数:'))
count = 1
quotient = num
while True:
quotient = quotient // 10
if quotient == 0:
print('这是一个',count,'位数')
break
count +=1
for i in range(1,count+1):
print(num // 10**(i-1) % 10)