# Weekend homework - circular exercises

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

```count = 0
print('101-200 There are prime numbers between')
for num in range(101, 201):
for x in range(2, int(num**0.5 + 1)):
if num % x == 0:
break
else:
print(num, end=' ')
count += 1
print()
print('share', count, 'individual')
```

#### 2. Find the cumulative value of integers 1 ~ 100, but it is required to skip all numbers with 3 bits.

```count = 0
for num in range(1, 101):
if num % 10 == 3:
continue
count += num
print('The cumulative value is', count)
```

#### 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

```molecule = 1
denominator = 1   # Fibonacci_ The last digit of n
for x in range(20):
molecule, denominator = denominator, molecule + denominator
print('The 20th score is:', denominator, '/', molecule)
```

#### 4. Write a program to calculate the factorial n of n! Results

```n = int(input('Please enter n: '))
count = 1
for x in range(1, n + 1):
count *= x
print('Factorial n! Results', count)
```

#### 5. Seek 1 + 2+ 3!+…+ 20! Sum of

```count = 1
sum1 = 0
for x in range(1, 21):
count *= x
sum1 += count
print('The result is', 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 = int(input('Please enter parameters a:'))
n = int(input('Please enter parameters n:'))
count = 0
for i in range(1, n+1):
count += a
a = a * 10 + a % 10
print(count)
```

#### 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 = int(input('Please enter parameters n:'))
for x in range(n):
for y in range(n, x, -1):
print('*', end='')
print()
```
```b.according to n The corresponding shape is output according to the different values of(n Odd number)
n = 5               n = 7
*                    *
***                  ***
*****                *****
*******
```
```n = int(input('Please enter parameters n:'))
while True:
if n % 2 == 0:
continue
else:
for x in range(n // 2 + 1):
for z in range(n // 2 - x):
print(end=' ')
for y in range(x * 2 + 1):
print('*', end='')
print()
break

```
```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
```
```n = int(input('Please enter parameters n:'))
for x in range(n):
for y in range(n-x):
print(' ', end='')
for z in range(1, x+2):
print(z, end='')
for z in range(x, 0, -1):
print(z, end='')
print()
```

#### 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?

```shampoo = 0    # shampoo
soap = 0       # Soap
toothbrush = 0  # toothbrush
for shampoo in range(1, 7):
for soap in range(1, 51):
toothbrush = (100 - shampoo * 15 - soap * 2) // 5
if (100 - shampoo*15 - soap*2) % 5 == 0 and toothbrush > 0:
print('shampoo:', shampoo, 'Soap:', soap, 'toothbrush:', toothbrush)
```

#### 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)?

```count = 0
n = 1
unit = 8e-5
while count < 8848.13:
count = unit * 2**n
n += 1
print(n)
```

#### 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?

```mouth0 = 1      # Number of rabbits in the first month
mouth1 = 1      # Number of rabbits in the second month
n = int(input('Please enter the month'))
if n == 1 or n == 2:    # In the first two months, the rabbits were not adults, and the total number of rabbits was 2
print('In the first two months, the rabbits were not adults, and the total number of rabbits was 2')
else:
for x in range(3, n+1):
mouth1, mouth0 = mouth0 + mouth1, mouth1
print('The first', n, 'Month rabbit has', 2*mouth1, 'only')
```

#### 11. Decompose a positive integer into prime factors. For example, enter 90 and print out 90=2x3x3x5.

```num = int(input('Please enter a positive integer:'))
sum1 = num
temp = []           # List of storage factors
while sum1 != 1:
for x in range(2, num + 1):
if sum1 % x == 0:
temp.append(x)       # Get a factor and add it to the temp list
sum1 //=x # replaces sum1 with another factor until sum1 becomes 1;
if sum1 != 1:
temp.append('x')    # Every time you add a factor, add an x, and don't add it the last time
break
if len(temp) == 1:   # There is only one element in the temp list, and the number entered on the surface is a prime number
print('This is a prime number')
else:
print(num, '=', end='', sep='')
for x in temp:
print(x, end='')                    #  Output as specified
```

### 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

```while True:
if len(number) == 4:
number = int(number)
qian = number // 1000
bai = number // 100 % 10
shi = number // 10 % 10
ge = number % 10
qian, ge = (ge + 5) % 10, (qian + 5) % 10
bai, shi = (shi + 5) % 10, (bai + 5) % 10
number = qian*1000 + bai*100 + shi*10 + ge
print('Encrypted value', number)
break
else:
print('The number you entered is not a 4-digit integer. Please re-enter it')
```

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

```money = 10000
for x in range(1, 6):
money += money*0.003
print('5 The principal after years is', money, 'element')
```

### 14. Enter an integer and calculate the sum of its digits. (Note: the entered integer can be any bit)

```number = int(input('Please enter an integer'))
count = 0
while number != 0:
if number > 0:
count += number % 10
number = number // 10
else:
count += 10 - number % 10
number = number // 10 + 1
print('The sum of the numbers on it is:', count)
```

### 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)

```a = int(input('Please enter a number'))
b = int(input('Please enter another number'))
divisor = 1   # Common divisor
n = a if a < b else b
for x in range(2, n):
if a % x == 0 and b % x == 0:
divisor *= x
print(divisor)
print(a*b//divisor)
```

Tags: Python

Posted on Sat, 04 Dec 2021 00:52:12 -0500 by homchz