Algorithmic Learning (12)

1.Integer Factorization

Description: The basic theorem of the algorithm is that any integer can be expressed as the product of one or more prime numbers, such a representation is unique, for example:

1000 = 2 * 2 * 2 * 5 * 5 * 5
1001 = 7 * 11 * 13
1002 = 2 * 3 * 167
1003 = 17 * 59
1009 = 1009

Problem statement:

You'll get a few numbers and decompose them into the product of prime numbers.

Input data: The number of integers in the first row that contain the decomposition.

The following line contains an integer (no more than 13 digits in length).

Answer: The product of each integer should be included, expressed as p1*p2*p3, where p is some prime number in a non-descending order.Each product should be separated by a space.

Example:

input data:
5
1000
1001
1002
1003
1009

answer:
2*2*2*5*5*5 7*11*13 2*3*167 17*59 1009

Test data:

30
1529673724591
2340274453339
702466756897
829817084753
27819311669
186806051203
49953067651
34579886191
4695575168147
2100191285953
3716086062137
2556947487959
542629812761
4073412911317
3009916093333
587577561341
288846512519
68274514267
60010816097
2290843100603
2150048292487
72824930317
2821314585503
23823809797
1186835381663
253181641523
52136986493
5271995870831
1000835638687
654425636171

 

The code is as follows:

 1 test_cases = int(input())  # Number of test cases
 2 
 3 def eladuosai(n): # Eradoser sieving, construct prime numbers
 4     l = list(range(1, n+1))
 5     l[0] = 0
 6     for i in range(2, n+1):
 7         if l[i-1] != 0:
 8             for j in range(i*2, n+1, i):
 9                 l[j-1] = 0
10     result = [x for x in l if x != 0]
11     return result
12 
13 prime = eladuosai(3000)
14 
15 for n in range(test_cases):
16     date = int(input().split()[0])
17     p_list = []
18     for p in prime:
19         while date % p == 0:
20             date = date / p
21             p_list.append(p)
22         if p > date:
23             break 
24     print('*'.join(str(m) for m in p_list), end=' ')   # Format Output
25 
26 Output: 131*131*379*479*491 191*241*269*331*571 193*197*199*227*409 103*131*293*431*487 53*67*107*211*347 127*137*167*239*269 349*439*571*571 263*409*563*571 149*307*409*419*599 
     139*257*271*401*541 229*257*263*409*587 173*223*353*359*523 61*137*353*419*439 281*283*317*349*463 173*251*317*449*487 83*257*277*277*359 103*107*227*263*439 67*71*211*251*271
     53*113*127*257*307 131*193*373*421*577 89*227*419*499*509 53*83*227*233*313 151*211*349*487*521 193*401*541*569 101*239*271*419*433 71*137*211*281*439 89*109*113*199*239
     211*239*313*569*587 79*157*397*439*463 97*109*277*439*509

 

2.Mortgage Calculator (Loan Calculation)

Description: Mathematically, a mortgage loan works like this:

1. Loan a large sum of money from the bank;

2. The bank tells you its interest rate R, the rate at which debt is growing;

3. At the end of each month, the debt increased by R/12%;

4. After that, you provide the bank with some small pre-defined money to reduce its debts;

5. Debt is considered stable when its value drops to zero (which may take years).

For example, take P=800,000 from a bank, and pay M=10,000 at the end of each month at an interest rate of R=6%.

Month     P        P * (R/12)%      -M       new P
  1    $800000       $4000       -$10000    $794000
  2    $794000       $3970       -$10000    $787970
  3    $787970       $3940       -$10000    $781910
 ...
 12    $732325       $3662       -$10000    $725987
 ...
 24    $654138       $3271       -$10000    $647408
 ...
 103     $4188         $21        -$4209         $0

Therefore, after 103 months (approximately 8.5 years), the debt may be repaid.The final payment may, of course, be less than M (because P does not need to be negative).

Input data: Value L containing loan size P, interest rate R and time length, to be repaid within a few months.

Answer: It should include the M integer paid monthly (if you get a non-integer result, increase it to the nearest integer).

For example:

input data:
800000 6 103

answer:
9957

Test data:

600000 8 114

The code is as follows:

 1 data = input().split()
 2 P = int(data[0])
 3 R = int(data[1])
 4 L = int(data[2])
 5 
 6 def debt(p, r, t):
 7     # The amount of monthly repayment is: [ Principal x Monthly interest rate x(1+Monthly interest rate)Secondary of the number of months of loan ] / [(1+Monthly interest rate)The power of repayment months - 1] 
 8     m = round((p * (r / 1200) * (1 + r/1200) ** t) / ((1 + r/1200) ** t - 1))  
 9     return m
10 
11 case = debt(P, R, L)
12 print(case)
13 
14 Output: 7531

Keywords: Python calculator less

Added by gotit on Fri, 31 May 2019 19:59:51 +0300