Weekend homework - circular exercises

Posted by Amman-DJ on Sat, 04 Dec 2021 23:39:25 +0100

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

    for x in range(101,200):
        for a in range(2,x):
            if x%a==0:
                break
        else:
            print(x)
    
  2. Find the cumulative value of integers 1 ~ 100, but it is required to skip all numbers with 3 bits.

    sum=0
    for x in range(1,101):
        if x%10!=3:
            sum+=x
    print(sum)
    
    
  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

    num_1=1
    num_2=1
    n=5
    n=n+3
    a='num_2/num_1'
    if n>=3:
        for _ in range(n-3):
    
            num_1,num_2 =num_2,num_1+num_2
            a=(num_2 + num_1) / num_2
        print('The first',n-2,'number',a)
    
  4. Write a program to calculate the factorial n of n! Results

    sum=1
    n=int(input('please enter a number n: '))
    while n>1:
        if n>1:
            sum *= n
            n = n - 1
    print('n','The factorial of is:',sum)
    
  5. Seek 1 + 2+ 3!+…+ 20! Sum of

    sum=0
    for i in range(1,21):
        a=1
        for d in range(i-1,0,-1):
            a*=d
            i += 1
            sum+=a
    print(sum)
    
  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


n=int(input('Please enter the number of items:'))
a=int(input('Please enter a number:'))
i=1
num=a
sum=0
while i<n:
    num=num+a*10**i
    i+=1
    sum+=num
print(sum+a)
  1. Console output triangle

    a.according to n The corresponding shape is output according to the different values of
    n=int(input('Please enter a number n: '))
    for i in range(n,0,-1):
        for j in range(n,0,-1):
            print("*", end=" ")
        n-=1
        print()
    
    
    b.according to n The corresponding shape is output according to the different values of(n Odd number)
    n=int(input('Please enter a number n: '))
    for i in range(n):
        for k in range(n - i):
            print(" ", end=" ")
        for j in range(2 * i + 1):
            if ((j + 1) % 2) == 0:
                print(" ", end=" ")
            else:
                print("*", end=" ")
        print()
        
    c. according to n The corresponding shape is output according to the different values of
    ```python
    n=int(input('Please enter a number n: '))
    for i in range(n):
        x = 0
        for k in range(0,n - i):
            print(" ", end=" ")
        for j in range(0,2 * i + 1):
            print(x, end=" ")
            if x<=(k-1):
                x+=1
            else:
                x-=1
        print()
        #Can't do it
    
  2. 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 a in range(0,7):
        for b in range(0,51):
            for c in range(0,21):
                if a*15+b*2+c*5==100:
                    print(a,b,c)
    
  3. 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)?

    n=0.0008
    sum=0
    while n<=8848.13:
        n*=2
        sum+=1
    print(sum)
    
  4. 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?

    a, b = 0, 1
    for i in range(1, 13):
     print('The first',i,'Months:',b,'To rabbit')
     a, b = b, a+b
    
    a, b = 0, 1
    for i in range(1, 13):
     print('The first%s Months:%s Rabbit' % (i,b))
     a, b = b, a+b
    
  5. Decompose a positive integer into prime factors. For example, enter 90 and print out 90=2x3x3x5.

  6. 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

    n=int(input('Please enter a four digit integer:'))
    qian=n//1000
    bai=n//100%10
    shi=n//10%10
    ge=n%10
    qian=(qian+5)%10
    bai=(bai+5)%10
    shi=(shi+5)%10
    ge=(ge+5)%10
    print(ge,shi,bai,qian)
    
  7. 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.

    n=10000
    a=1
    x=0
    while a<=5:
        x=n*0.003
        n=n+x
        a+=1
    print(n)
    
  8. Enter an integer and calculate the sum of its digits. (Note: the entered integer can be any bit)

    num=int(input('Please enter a number:'))
    count = 0
    sum=0
    while True:
        n=num%10
        num //= 10    # num = num // 10
        sum += n
        if num == 0:
            break
    print(sum)
    
  9. 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)

    n1=24
    n2=36
    i=23
    while i <n1:
        if n1%i!=0:
            i-=1
        else:
            if n2%i==0:
                print(i)
                break
            else:
                i-=1
    
    n1 = 24
    n2 = 36
    i = 36
    while True:
        if i%n1!=0:
            i+=1
        else:
            if i%n2==0:
                print(i)
                break
            else:
                i+=1
    
    

Topics: Algorithm