1.读程序总结程序功能
# a.
numbers = 1
for i in range(0,20):
numbers *= 2
print(numbers)
'''
i == 0 numbers*=2 ==2numbers ==2**1
i == 1 numbers*=2 ==2numbers ==2**2
...
i == 19 numbers*=2 ==2numbers ==2**20
印刷 2**20
'''
# b.
summation = 0
num = 1
while num <= 100: #num<=100时循环
if (num%3 == 0 or num%7 == 0) and num%21 != 0: #能被3或者7整除并且不能被3和7同时整除的个数.
summation += 1
num += 1
print(summation)
# 编程实现(for和while各写一遍):
# 1.求1到100 之间所有数的和、平均值.
```python
# a.for循环
n = 1
time = 1
sum1 = 0
for n in range(1,101):
sum1 += n
time = sum1/n
print('和:',sum1, '平均值:', time)
# b.while循环
n = 1
time = 1
sum1 = 0
while n <= 100:
sum1 += n
time = sum1/n
n += 1
print('和2:',sum1, '平均值2:', time)
# 2. 计算1-100之间能3整除的数的和
# a.for
n = 1
m = 0
sum1 = 0
for n in range(1,101):
if n%3 == 0:
m += 1
sum1 += n
print('和3:',sum1)
# b.while
n = 1
m = 0
sum1 = 0
while n <= 100:
if n%3 == 0:
m += 1
sum1 += n
n+=1
print('和4:',sum1)
3. 计算1-100之间不能被7整除的数的和
# a.for
n=0
sum1=0
for n in range(1,101):
if n%7:
sum1 += n
print('和5:', sum1)
#b.while
n = 1
sum1 = 0
while n <= 100:
if n%7:
sum1 += n
n += 1
print('和6:', sum1)
1. 求斐波那契数列中第n个数的值:1,1,2,3,5,8,13,21,32.
f(n) = f(n-1)+ f(n-2)。
n=int(input('请输入大于1的项:'))
i=1
j=0
l=1
m=0
while True:
l+=j
i+=1
if n==i:
break
j+=l
i+=1
if n==i:
break
m=j+l
print(m)
2.判断101-200之间有多少个素数,并输出所有素数。判断素数的数),如果能被整除,则表明此数不是素数,反之是素数
print('=======')
i=0
for n in range(101,201):
m=2
while n%m :
m+=1
if n==m:
i+=1
print(n, i)
3.打印出所有的水仙花数,所谓水仙花数是指一个三位数,
其各位数字一个水仙花数,因为153 = 1^3 + 5^3 + 3^3
for n in range(100,1000):
g = n%10 #个位
s = n%100//10 #十位
b = n//100 #百位
if b**3 + s**3 + g**3 == n:
print(n)
4. 有⼀分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的第20分⼦:
上⼀个分数的分⼦加分⺟ 分⺟: 上⼀个分数的分⼦ fz = 2 fm
5. 给⼀个正整数要求:1、求它是⼏位数 2.逆序打印出各位数字
n=int(input('请输入一个正整数'))
n=int(input('请输入一个正整数'))
m=1
num=0
s=0
s1=0
while True:
if n/m>=1:
m*=10
s = n%m
sum1 = (s-s1)/10**num
s1 = s
num+=1
print('逆序打印:',int(sum1))
else:
print('位数:',num)
break