from math import log, ceil balance = 320000 annualInterestRate = 0.2 monthlyInterestRate = annualInterestRate / 12 lower = balance / 12 upper = (balance * pow(1 + monthlyInterestRate,12))/12 def paidMonth(pay): ''' Given a specified monthly payment, returns the month in which the balance will be paid off. ''' mo = 0 bal = balance while bal>0: bal = (bal - pay)*(1+monthlyInterestRate) mo+=1 #print 'mo:',mo return mo def midpoint(a,b): return (a+b)/2 # initial guess = halfway between the bounds payment = midpoint(upper,lower) # for "nearest cent resolution" we need to examine all the cents between upper and lower cents = (upper-lower)*100 + 1 #print 'cents:',cents iters = int(ceil(log(cents,2))) #print 'iters:',iters for i in range(iters): #print 'payment:',payment if paidMonth(payment)<=12: upper = payment payment = midpoint(payment,lower) else: lower=payment payment = midpoint(payment,upper) print 'Lowest Payment:',("%.2f" % payment)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question