Python programming exercises (3)

Table of contents

1. Find the sum of numbers between 1 and 100 that are not divisible by 3

2. Given a positive integer N, find the sum of all prime numbers between 1 and N (inclusive)

 3. Calculate PI (the formula is as follows: PI=4 (1-1/3+1/5-1/7+1/9-.....)

 4. Find a+aa+aaa+......+aaaaaaaaa=? Where a is a number from 1 to 9, and the number of terms must also be specified.

 5. Find a number (function) within 10,000 that is divisible by 5 or 6, but not both at the same time.

 6. Merge two ordered arrays, and the merged array will still be an ordered list.

 7. Write a method to calculate the sum of all even-numbered elements in the list (note the return value)

 8. Given an array A of non-negative integers, place all even numbers in the array before the odd elements


1. Find the sum of numbers between 1 and 100 that are not divisible by 3

sum = 0
for a in range (1, 101):
    if a % 3 == 0:
        continue
    else:
        sum += a

print(sum)

2. Given a positive integer N, find the sum of all prime numbers between 1 and N (inclusive)

def xy(x):
    if x == 1:
        return False
    for i in range(2, x // 2 + 1):
        if x % i == 0:
            return False
    return True


N = int(input('请输入一个正整数N:'))
sum = 0
for x in range(1, N + 1):
    if xy(x):
        sum += x
print(f'1到N(含)之间所有质数的总和为:{
   
   

 3. Calculate PI (the formula is as follows: PI=4 (1-1/3+1/5-1/7+1/9-.....)

def PI():
    n = 0
    sum_PI = 0
    for i in range(1, 10000, 2):
        sum_PI += ((-1) ** n) * (1 / i)
        n += 1
    PI = 4 * sum_PI
    return PI


print(f'PI = {PI()}')

 4. Find a+aa+aaa+......+aaaaaaaaa=? Where a is a number from 1 to 9, and the number of terms must also be specified.

def sum(a, n):
    sum_a = 0
    for i in range(1, n + 1):
        num = int(f'{a}' * i)
        sum_a += num
    return sum_a


a = int(input('请输入一个在区间[1,9]的正整数:'))
n = int(input('请输入指定的项数:'))
print(f'多项式的和为:{sum(a, n)}')

 5. Find a number (function) within 10,000 that is divisible by 5 or 6, but not both at the same time.

def func():
    for i in range(1, 10001):
        if (i % 5 == 0 or i % 6 == 0 ):
            if i % 5 == 0 and i % 6 == 0:
                continue
        print(i)
func()

 6. Merge two ordered arrays, and the merged array will still be an ordered list.

arr1 = [1, 3, 4, 6, 10]
arr2 = [2, 5, 8, 11]
ans = arr1 + arr2
ans.sort()       
print(ans)

 7. Write a method to calculate the sum of all even-numbered elements in the list (note the return value)

arr = [1, 2, 3, 4, 6, 10, 11, 13]
sum = 0
for i in arr:
    if i % 2 != 0:
        continue
    else:
        sum += i
print(sum)

 8. Given an array A of non-negative integers, place all even numbers in the array before the odd elements

def even_before_odd(ls = []):
    for i in range(len(ls)):
        if ls[i] % 2 != 0:
            for j in range(i + 1, len(ls)):
                if ls[j] % 2 == 0:
                    ls[i], ls[j] = ls[j], ls[i]
                    break
    return ls


ls = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(f'原列表为:{ls}')
print(f'将偶数置于奇数前,列表变为:{even_before_odd(ls)}')

Guess you like

Origin blog.csdn.net/weixin_62304542/article/details/129800809