Algorithm Learning (12), algorithm Learning
1. Integer Factorization (Factorization)
Note: The basic theorem of an algorithm. Any integer can be expressed as the product of one or more prime numbers. Such an integer is unique, for example:
1000 = 2 * 2 * 2 * 5 * 5 * 51001 = 7 * 11 * 131002 = 2 * 3 * 1671003 = 17 * 591009 = 1009
Problem description:
You will get several numbers to break them into the product of prime numbers.
Input data: the number of decomposed integers in the first line.
The following row contains an integer (no more than 13 characters ).
Answer: It should contain the product of each integer, expressedp1*p2*p3,P is a prime number in non-descending order. Each product should be separated by spaces.
Example:
input data:510001001100210031009answer:2*2*2*5*5*5 7*11*13 2*3*167 17*59 1009
Test data:
30152967372459123402744533397024667568978298170847532781931166918680605120349953067651345798861914695575168147210019128595337160860621372556947487959542629812761407341291131730099160933335875775613412888465125196827451426760010816097229084310060321500482924877282493031728213145855032382380979711868353816632531816415235213698649352719958708311000835638687654425636171
The Code is as follows:
1 test_cases = int (input () # Number of test cases 2 3 def eladuosai (n): # irad sieve method, constructing a prime number 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] = 010 result = [x for x in l if x! = 0] 11 return result12 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/p2p_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 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 61*523*137*353*419*439*281*283*317*349*463 173*251*317*449*487 83*257*277*277*359*103*107*227*263 67*71*439*211*251*271
53*113*127*257*307*131*193*373*421*577*227*419*499*509 53*83*227*233*313 151*211*349*487*521 193*401*541*569*101*239*271*419 71*433*137*211*281 89*439*109*113*199*239 *
211*239*313*569*587 79*157*397*439*463 97*109*277*439*509
2. Mortgage Calculator (loan calculation)
Note: the working principle of a mortgage loan is as follows:
1. borrowed a large amount of money from the bank;
2. The Bank tells you its interest rate R, that is, the debt growth rate;
3. At the end of each month, the debt increased by R/12%;
4. After that, you have provided some pre-defined small money to the bank to reduce the debt;
5. Debt is considered stable when its value falls to zero (which may take years ).
For example, take P = $0.8 million from a bank, interest rate R = 6%, and pay M = $10000 at the end of each month.
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, the debt may be repaid after 103 months (about 8.5 years. Of course, the final payment may be less than M (because P does not need to be converted into a negative number ).
Input data: contains the loan size P, interest rate R, and time length value L, which will be repaid within a few months.
Answer: it should include the M integer paid every month (if you get a non-integer result, add it to the nearest integer ).
For example:
input data:800000 6 103answer: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 monthly repayment amount is [the power of the principal x monthly interest rate x (1 + monthly interest rate) Loan month]/[(1 + monthly interest rate) the power of the number of months for repayment-1] 8 m = round (p * (r/1200) * (1 + r/1200) ** t) /(1 + r/1200) ** t-1) 9 return m10 11 case = debt (P, R, L) 12 print (case) 13 14 output: 7531