day_4_Python作业

读程序,总结程序的功能:

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

求出在1到100中能够被3或7整除但是不能被21整除的个数。

编程实现(for和while各写⼀一遍):

1. 求1到100之间所有数的和、平均值

sum = 0
for i in range(1, 101):
    sum += i
average = sum / 100
print("1到100的和为:", sum, "平均数为:", average)
sum = 0
i = 1
while i<101:
    sum += i
    i += 1
average = sum / 100
print("1到100的和为:", sum, "平均数为:", average)
题1

2. 计算1-100之间能3整除的数的和

sum = 0
for i in range(0, 101, 3):
    if i%3 == 0:
        sum += i
        print(i)
print("1到100之间能3整除的数的和为:", sum)
i = 0
sum = 0
while True:
    i += 1
    if i % 3 == 0:
        sum += i
    if i == 100:
        break
print("1到100之间能3整除的数的和为:", sum)
题1

3. 计算1-100之间不能被7整除的数的和

sum = 0
for i in range(1, 101):
    if i % 7 != 0:
        sum += i
print("1-100之间不不能被7整除的数的和为:", sum)
sum = 0
i = 0
while True:
    i += 1
    if i % 7 != 0:
        sum += i
    if i == 100:
        break
print("1-100之间不不能被7整除的数的和为:", sum)
题3

困难

1. 求斐波那契数列列中第n个数的值:1,1,2,3,5,8,13,21,34....

value = 1
aa = 1
count = 2
flag = 1
N = int(input("请输入N的值:"))
if N == 1 or N == 2:
    value = 1
else:
    while True:
        if flag == 1:
            bb = value
            value += aa
            flag = 2
        else:
            flag = 1
            aa = value
            value += bb
        count += 1
        if N == count:
            break
print(value)
斐波那契数列

2. 判断101-200之间有多少个素数,并输出所有素数。判断素数的⽅方法:⽤用⼀一个数分别除2到sqrt(这个 数),如果能被整除,则表明此数不不是素数,反之是素数

aa = 0
for i in range(101, 201):
    count = 1
    for j in range(2, int((i**(1/2)//1))+1):
        if i % j == 0:
            count += 1
            break
    if count == 1:
        print(i)
        aa += 1
print('素数总共有', aa, '个')
素数

3. 打印出所有的水仙花数,所谓水仙花数是指一个三位数,其各位数字立方和等于该数本身。例如:153是 一个水仙花数,因为153 = 1^3 + 5^3 + 3^3

for i in range(100, 1000):
    ge = i % 10
    shi = i % 100 // 10
    bai = i // 100
    if i == ge**3 + shi**3 + bai**3:
        print(i)
水仙花数

4. 有一分数序列列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列列的第20个分数

fenzi = 2
fenmu = 1
aa = fenzi
bb = fenmu
cc = fenzi
dd = fenmu
flag = 1
n = 20
if n > 1:
    for i in range((n-1)*2):
        if flag == 1:
            cc = fenzi
            dd = fenmu
            fenzi = aa + bb
            fenmu = aa
            flag = 2
        else:
            aa = fenzi
            bb = fenmu
            fenzi = cc +dd
            fenmu = cc
            flag = 1
print(fenzi, fenmu)
第20个数列

5. 给一个正整数,要求:1、求它是几位数 2.逆序打印出各位数

num = int(input("请输入一个数:"))
count = 0
while True:
    print(num % 10, end='')
    num = num // 10
    count += 1
    if num == 0:
        break
print('输入数字是:', count, '位数')
倒叙输出

你可能感兴趣的:(day_4_Python作业)