读程序,总结程序的功能:
1.
numbers=1
for i in range(0,20):
numbers*=2
print(numbers)
程序可以写作number=1*2^20 ,所以就是求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)
程序求0到100中是3或7的倍数但不是21倍数的数的个数
结果为39
2.1. 求1到100之间所有数的和、平均值
sum1 = 0
for i in range(1,101):
sum1 += i
average = sum1/100
print(sum1)
print(average)
sum2 = 0
i = 1
while i<101:
sum2 += i
i += 1
average = sum2/100
print(sum2)
print(average)
2.22. 计算1-100之间能3整除的数的和
sum3 = 0
for x in range(1,101):
if x%3 == 0:
sum3 += x
print(sum3)
i = 1
sum4 = 0
while i < 101:
if i%3 == 0:
sum4 += i
i += 1
print(sum4)
2.33. 计算1-100之间不能被7整除的数的和
sum5 = 0
for num in range(1,101):
if num%7 != 0:
sum5 += num
print(sum5)
sum6 = 0
n = 1
while n < 101:
if n%7 != 0:
sum6 += n
n += 1
print(sum6)
3.11. 求斐波那契数列中第n个数的值:1,1,2,3,5,8,13,21,34....
in_num = input('请输入一个整')
con = int(in_num)
i = 0
n = 1
for m in range(0,con):
x = i + n
n = i
i = x
print(i)
3.22. 判断101-200之间有多少个素数,并输出所有素数。判断素数的⽅法:⽤⼀个数分别除2到sqrt(这个
数),如果能被整除,则表明此数不是素数,反之是素数
d = 0
for a in range(101,201):
c = 0
for b in range(2,a):
if a%b == 0:
c += 1
if c == 0:
d += 1
print(a)
print(d)
3.3. 打印出所有的⽔仙花数,所谓⽔仙花数是指⼀个三位数,其各位数字⽴⽅和等于该数本身。例如:153是
⼀个⽔仙花数,因为153 = 1^3 + 5^3 + 3^3
for x in range(100,1000):
a = x % 10
b = x // 10 % 10
c = x // 100
if x == a ** 3 + b ** 3 + c ** 3:
print(x)
3.4. 有⼀分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的第20个分数
分⼦:上⼀个分数的分⼦加分⺟ 分⺟: 上⼀个分数的分⼦ fz = 2 fm = 1 fz+fm / fz
con = 6
i = 1
n = 1
for m in range(0,con):
x = i + n
n = i
i = x
print(i)
a = 0
b = 1
for c in range(0,con+1):
d = a + b
b = a
a = d
print(a)
print(i/a)
3.5. 给⼀个正整数,要求:1、求它是⼏位数 2.逆序打印出各位数字
in_num = input('请输入一个正整数:')
con = len(in_num)
print(con)
print(in_num[::-1])
网友评论