Python作业一

目录

1、用循环语句求1+22+333+4444+55555的和

 2、求出2000-2100的所有闰年,条件是四年一闰,百年不闰,四百年再闰

3、输入两个正整数,并求出它们的最大公约数和最小公倍数

4、求出100以内的所有质数

5、求100以内最大的10个质数的和

6、求1到10所有的偶数和

7、将10-20不能被2或3整除的数输出


1、用循环语句求1+22+333+4444+55555的和

for i in range(1,6):
    a = str(i)
    lst.append(a)
for j in range(0,5):
    b = int(lst[j] * (j + 1))
    sum += b
print("sum = ",sum)

运行结果:

F:\pythonProject\venv\Scripts\python.exe F:\pythonProject\yunjisuan\0719homework\04.py 
sum =  60355

Process finished with exit code 0

 2、求出2000-2100的所有闰年,条件是四年一闰,百年不闰,四百年再闰

lst = []
for i in range(2000,2101):
    if i%4 == 0 and i%100 != 0 or i%400 == 0:
        lst.append(i)
print(lst)

运行结果:

F:\pythonProject\venv\Scripts\python.exe F:\pythonProject\yunjisuan\0719homework\05.py 
[2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, 2048, 2052, 2056, 2060, 2064, 2068, 2072, 2076, 2080, 2084, 2088, 2092, 2096]

Process finished with exit code 0

3、输入两个正整数,并求出它们的最大公约数和最小公倍数

a = int(input("请输入第一个整数:"))
b = int(input("请输入第二个整数:"))
if a > b:
    a , b = b , a
for i in range(a,0,-1):
    if a % i == 0 and b % i == 0:
        print("%d和%d的最大公约数是%d"%(a , b , i))
        print("%d和%d的最小公倍数是%d"%(a , b , a * b / i))
        break

运行结果:

F:\pythonProject\venv\Scripts\python.exe F:\pythonProject\yunjisuan\0719homework\06.py 
请输入第一个整数:20
请输入第二个整数:26
20和26的最大公约数是2
20和26的最小公倍数是260

Process finished with exit code 0

4、求出100以内的所有质数

lst = []
flag = True
for i in range(2,101):
    for j in range(2,i):
        if i % j == 0:
            flag = False
            break
        else:
            flag = True
    if flag == True:
        lst.append(i)
print(lst)

运行结果:

F:\pythonProject\venv\Scripts\python.exe F:\pythonProject\yunjisuan\0719homework\07.py 
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

Process finished with exit code 0

5、求100以内最大的10个质数的和

flag =True
sum = 0
cnt = 0
for i in range(100,1,-1):
    for j in range(2,i):
        if i % j == 0:
            flag = False
            break
        else:
            flag = True
    if flag == True:
        sum += i
        cnt += 1
    if cnt == 10:
        print("100以内最大的10个质数的和为:",sum)
        break

运行结果:

F:\pythonProject\venv\Scripts\python.exe F:\pythonProject\yunjisuan\0719homework\08.py 
100以内最大的10个质数的和为: 732

Process finished with exit code 0

6、求1到10所有的偶数和

sum = 0
for i in range(1,11):
    if i % 2 == 0:
        sum += i
print("1到10所有的偶数和为:",sum)

运行结果:

F:\pythonProject\venv\Scripts\python.exe F:\pythonProject\yunjisuan\0719homework\09.py 
1到10所有的偶数和为: 30

Process finished with exit code 0

7、将10-20不能被2或3整除的数输出

for i in range(10,21):
    if i % 2 != 0 and i % 3 != 0:
        print(i)

运行结果:

F:\pythonProject\venv\Scripts\python.exe F:\pythonProject\yunjisuan\0719homework\10.py 
11
13
17
19

Process finished with exit code 0

你可能感兴趣的:(python,算法,开发语言)