Weekend homework - circular exercises

Keywords: Python

  1. Judge how many primes there are between 101-200, and output all primes.

    count = 0
    for x in range(100, 200):
        for y in range(2, int(x ** 0.5)+1):
            if x % y == 0:
                break
        else:
            print(x)
            count += 1
    print('have', count, 'Prime number', sep='')
    
  2. Find the cumulative value of integers 1 ~ 100, but it is required to skip all numbers with 3 bits.

    sum1 = 0
    for x in range(101):
        if x % 10 != 3:
            sum1 += x
    print(sum1)
    
  3. There is a sequence of fractions: 2 / 1, 3 / 2, 5 / 3, 8 / 5, 13 / 8, 21 / 13... Find the 20th fraction of this sequence

    x = y = 1
    z = sum2 = 0
    for num in range(20):
        sum1 = x + y
        sum2 = z + y
        x, y = sum2, sum1
    print(sum1, '/', sum2, sep='')
    
  4. Write a program to calculate the factorial n of n! Results

    n = int(input('Enter a number:'))
    sum1 = 1
    sum2 = 0
    while True:
        sum2 += 1
        sum1 *= sum2
        if sum2 == n:
            break
    print(sum1)
    
  5. Please 1+ 2!+ 3!+…+ 20! Sum of

    sum1 = 0
    sum2 = 1
    for x in range(1, 21):
        sum2 *= x
        sum1 += sum2
    print(sum1)
    
  6. Write a program to find the result of the expression a + aa + aaa + aaaa +... Where a is a number from 1 to 9, and the number of summation items is controlled by n. (A and N can be expressed as variables)

    For example, when a is 3 and n is 5, 3 + 33 + 333 + 3333 + 33333

    a = 3
    n = 5
    sum1 = sum2 = 0
    for x in range(n):
        sum1 += a*10**(n-x-1)
        sum2 += sum1//10**(n-x)
    print(sum1+sum2)
    
  7. Console output triangle

    a.according to n The corresponding shape is output according to the different values of
    n = 5 Time             n = 4
    *****               ****
    ****                ***
    ***                 **
    **                  *
    *
    
    n = 5
    for x in range(n):
        print('*' * (n-x))
    
    b.according to n The corresponding shape is output according to the different values of(n Odd number)
    n = 5               n = 7
      *                    *
     ***                  ***
    *****                *****
                        *******
    
    n = 7
    for x in range(1, n+1):
        if x % 2:
            print(('*' * x).center(n))
    
    c. according to n The corresponding shape is output according to the different values of
    n = 4
       1
      121
     12321
    1234321
    
    n = 5
        1
       121
      12321
     1234321
    123454321
    
  8. Xiaoming's unit issued a 100 yuan shopping card. Xiaoming went to the supermarket to buy three kinds of washing products, shampoo (15 yuan), soap (2 yuan) and toothbrush (5 yuan). If you want to spend 100 yuan, what combination can you buy?

    for x in range(1, 100 // 15 + 1):
        for y in range(1, 100 // 2 + 1):
            for z in range(1, 100 // 5 + 1):
                if x*15 + y*2 + z*5 == 100:
                    print(x, y, z)
    
  9. The thickness of a piece of paper is about 0.08mm. How many times can it be folded in half to reach the height of Mount Everest (8848.13m)?

    x = 0.08 / 1000
    count1 = 0
    while x < 8848.13:
        x *= 2
        count1 += 1
    print(count1)
    
  10. Classical question: a pair of rabbits give birth to a pair of rabbits every month from the third month after birth. The little rabbit grows to another pair of rabbits every month after the third month. If the rabbits don't die, what is the total number of rabbits every month?

    m = 3   # The default is from the 3rd month
    m2 = m1 = 0
    m3 = m0 = 1
    n = 10  # How many months was the rabbit
    if n < 3:
        print(1)  # In the first three months, there was only one pair of rabbits
    else:
        while True:
            m3 += m2
            m2, m1, m0 = m1, m0, m3
            m += 1
            if m == n:
                print(m0 + m1 + m2 + m3)  # There were so many rabbits in the nth month
                break
    
  11. Decompose a positive integer into prime factors. For example, enter 90 and print out 90=2x3x3x5.

  12. A company uses a public telephone to transmit data. The data is a four digit integer and is encrypted in the transmission process. The encryption rules are as follows: add 5 to each number, then replace the number with the remainder of sum divided by 10, and then exchange the first and fourth bits, and the second and third bits. Find the encrypted value of the input four bit integer

    tel1 = 1572
    list1 = []
    for x in str(tel1):
        x = (int(x)+5) % 10
        list1.append(x)
    list1[0], list1[3] = list1[3], list1[0]
    list1[1], list1[2] = list1[2], list1[1]
    print(list1)
    
  13. The principal of 10000 yuan is deposited in the bank with an annual interest rate of 3%. Every one year, add the principal and interest as the new principal. Calculate the principal obtained after 5 years.

    x = 10000
    for m in range(5):
        x += x * 0.003
    print(x)
    
  14. Enter an integer and calculate the sum of its digits. (Note: the entered integer can be any bit)

    num1 = 172
    sum1 = 0
    for x in str(num1):
        sum1 += int(x)
    print(sum1)
    
  15. Find the maximum common divisor and minimum common multiple of two numbers. (Note: the common divisor must be less than or equal to the smaller of the two numbers, and can be divided by the two numbers at the same time; the common multiple must be greater than or equal to the larger of the two numbers, and the multiple of the larger number can be divided by the decimals of the two numbers)

Posted by surfsup on Fri, 22 Oct 2021 21:25:39 -0700