读程序,总结程序的功能:
1.
numbers=1
for i in range(0,20):
numbers*=2
print(numbers)
计算2的20次方得到结果为1048576
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)
在0~100中有多少个能被3或者7整除的数,但不能同时被3和7整除
编程实现(for和while各写⼀一遍):
1. 求1到100之间所有数的和、平均值
n=0
for num in range(0,101):
n+=num
print(n)
print(n/100)
n = 0
count=0
while count < 100:
count+=1
n += count
print(n)
print(n/100)
2. 计算1-100之间能3整除的数的和
n = 0
count=0
while count < 100:
count+=1
if count%3==0:
n += count
print(n)
sum1 = 0
for x in range(3,101,3):
sum1 += x
print(sum1)
n = 0
count=0
for count in range(0,101):
count+=1
if count%3==0:
n += count
print(n)
3. 计算1-100之间不不能被7整除的数的和
n = 0
count=0
for count in range(0,100):
count+=1
if count%7!=0:
n += count
print(n)
n = 0
count=0
while count<100:
count+=1
if count%7!=0:
n += count
print(n)
1. 求斐波那契数列列中第n个数的值:1,1,2,3,5,8,13,21,34....
规则:从第二个数开始,后面的数的值就是这个数前两个数的和
n=8
n1=1
n2=1
sum =n1+n2
if n==1:
print(n1)
elif n==2:
print(n2)
else:
for x in range(n-3):
n1 = n2
n2 = sum
sum=n1+n2
print(sum)
=======
current = 1
pre_1 = 1
pre_2 = 0
n=7
for x in range(n-1):
current = pre_1+pre_2
pre_2= pre_1
pre_1=current
print(current)
2. 判断101-200之间有多少个素数,并输出所有素数。判断素数的方法:用一个数分别除2到sqrt(这个数),如果能被整除,则表明此数不不是素数,反之是素数
for x in range(101,201):
for a in range(2,x):
if x % a==0:
# print(x,'不是素数')
break
else:
print(x,'是素数')
for num in range(101,201):
is_prime = True
for x in range(2,num-1):
if num % x == 0:
is_prime = False
break
if is_prime:
print(num,'是素数')
3. 打印出所有的水仙花数,所谓水仙花数是指⼀一个三位数,其各位数字立方和等于该数本身。例如:153是⼀一个水仙花数,因为153 = 1^3 + 5^3 + 3^3
num=100
while num <999:
num+=1
ge = num%10
shi = num//100
bai = num//10%10
if ge**3+shi**3+bai**3==num:
print(num)
4. 有一分数序列列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列列的第20个分数分子:上一个分数的分子加分母 分母: 上一个分数的子 fz = 2 fm = 1 fz+fm / fz
fen_zi=1
fen_mu=1
n=20
for x in range(n):
fen_zi,fen_mu=fen_zi+fen_mu,fen_zi
print(fen_zi,'/',fen_mu)
5. 给一个正整数,要求:1、求它是几位数 2.逆序打印出各位数字
n=21321
count=0
while True:
print(n%10)
n //=10
count +=1
if n == 0:
break
print(count)