2012-10-17 4 views
0

나는이 코드를 찾았는데, 잘 돌아 간다. 놀랍게도 (MIT 코스의 경우 수표를 통과) 연간 이자율이 0.15 일 때만 실패합니다. 다른 경우에는 OK입니다. 저는 꽤 초보자입니다. 그래서 오늘은 그것을 해결할 희망이 없지만, 누군가가 저에게이 문제에 관해 밝혀 줄 수 있다면, 정말로 감사 할 것입니다!MIT의 "bisection search"

balance=294272 
annualInterestRate=0.15 

payment=(balance * (1 + annualInterestRate/12)**12)/12 
count=0 
total=0 
inicialbalance=balance 
while balance>0: 
    for month in range(1,13): 
     interest=(balance-payment)*(annualInterestRate/12) 
     balance=(balance-payment)*(1+(annualInterestRate/12)) 
     total=total+payment 
     finalbalance=balance-total 
     if balance <=0: 
      print('Balance Paid Off @ '+str(round(payment,2)) + ' Total Paid is:  '+str(round(total,2))+ ' ending balance: ' +str(round(balance,2))) 
      print('Lowest Payment: ' + str(round(payment,2))) 
      break 
    if balance <-0.01 or balance >0.01: 
     print('Payment at ' + str(payment) + ' ending balance : ' + str(balance)) 
     print('Payment will adjust to: ' + str(payment + 10)) 
     payment=payment+(balance/12) 
     count=count+1 
     balance=inicialbalance 
    total=0 
    #if count >200: 
     #print('count is > ' +str(count)+ ' aborting') 
     #break 
    #if balance <=0: 
     #print('balance paid off at ' + str(payment) + ' after ' + str(mth)) 
print('Lowest Payment: ' + str(round(payment,2)))` 

결과 :

{Balance Paid Off @ 42279.71 Total Paid is: 465076.8 ending balance: -359.78 
Lowest Payment: 42279.71 
Payment at 42279.7094717 ending balance : -359.781977705 
Payment will adjust to: 42289.7094717 
Balance Paid Off @ 42249.73 Total Paid is: 464747.0 ending balance: -4.19 
Lowest Payment: 42249.73 
Payment at 42249.7276402 ending balance : -4.18662043777 
Payment will adjust to: 42259.7276402 
Balance Paid Off @ 42249.38 Total Paid is: 464743.17 ending balance: -0.05 
Lowest Payment: 42249.38 
Payment at 42249.3787552 ending balance : -0.0487178117574 
Payment will adjust to: 42259.3787552 
Balance Paid Off @ 42249.37 Total Paid is: 464743.12 ending balance: -0.0 
Lowest Payment: 42249.37 
balance paid off at 42249.3746954 after 11 
Lowest Payment: 42249.37} 
+3

어떻게이 "실패"않습니다를 얻을? 오류가 있거나 잘못 나온 결과가 있습니까? 어떤 오류가 있습니까? – sinelaw

+0

죄송합니다. 제 질문에 분명해야했습니다. 이자율 (연간)이 0.15 인 몇 가지 예외를 제외하고 모든 무작위 테스트 사례에 대해 올바른 결과를 얻을 수 있습니다. 나는 그 점을 발견하지 못한다. 그러나 이것은 다음과 같다. ( –

+0

당신은 어떤 결과를 얻습니까? * 기대하는 것은 무엇입니까? 당신이 자원하는 정보가 많을수록 도움을 줄 수있는 사람이 더 많습니다. – sinelaw

답변

1

나는 for month in range(1,13): 내부

if balance <=0: 
    break 

잘못 조건을 생각한다. 지불 금액이 너무 많아서 12 회 지불 대신 11 회 지불 후 잔액이 마이너스가되면 루프가 일찍 중단됩니다. 나는 그것이 42249.37의 지나치게 큰 지불로 일어나고있는 것이라고 생각한다.

balance = 437092 
annualInterestRate = 0.15 
payment=(balance * (1 + annualInterestRate/12)**12)/12 
count=0 
total=0 
inicialbalance=balance 
while balance>0: 
    for month in range(1,13): 
     interest=(balance-payment)*(annualInterestRate/12) 
     balance = (balance-payment) + interest 
     total += payment 
     finalbalance = balance-total 
    if balance <-0.01 or balance>0.01: 
     print('Payment at ' + str(payment) + ' ending balance : ' + str(balance)) 
     payment=payment+(balance/12) 
     print('Payment will adjust to: ' + str(payment)) 
     count=count+1 
     balance=inicialbalance 
    else: 
     break 
    total=0 
print('Lowest Payment: ' + str(round(payment,2))) 

Payment at 42279.7094717 ending balance : -43172.4850925 
Payment will adjust to: 38682.0023807 
Payment at 38682.0023807 ending balance : 3673.67604215 
Payment will adjust to: 38988.1420508 
Payment at 38988.1420508 ending balance : -312.604095728 
Payment will adjust to: 38962.0917095 
Payment at 38962.0917095 ending balance : 26.6004186392 
Payment will adjust to: 38964.3084111 
Payment at 38964.3084111 ending balance : -2.26350928031 
Payment will adjust to: 38964.1197853 
Payment at 38964.1197853 ending balance : 0.192608783082 
Payment will adjust to: 38964.1358361 
Payment at 38964.1358361 ending balance : -0.0163896580426 
Payment will adjust to: 38964.1344702 
Lowest Payment: 38964.13