title: Python: smarter way to calculate loan payments
body: <p>How to calculate the monthly fee on a loan?</p>
  
  <p>Given is:</p>
  
  <ul>
  <li>a: an amount to loan.</li>
  <li>b: the loan period (number of months).</li>
  <li>c: the interest rate p.a. (interests is calculated and added every month, 1/12 of the interest is added. So if the interest is on 12%, 1% interest is added every month).</li>
  <li>d: the amount of money owed after the end of the period.</li>
  </ul>
  
  <p>This problem is a bit different than the usual since, the goal is not to have the loan payed after the loan period has ended, but to still owe an amount that is given. I have been able to find an algorithm so solve the problem if I wanted to pay the entire amount, but it will of course not work for this problem where the goal is to end up owing a given amount rather than not owing anything.</p>
  
  <p>I managed to make a solution to this problem by starting with an guess and then keep on improving that guess until it was close enough. I wondered however, if there is a better way to simply calculate this, rather than just guessing.</p>
  
  <p><strong>Edit: Here's how I'm doing it now.</strong></p>
  
  <pre><code>def find_payment(start, end, months, interest):
      difference = start
      guess = int(start / months * interest)
      while True:
          total = start
          for month in range(1, months + 1):
              ascribe = total * interest / 12
              total = total + ascribe - guess
          difference = total - end
          # See if the guess was good enough.
          if abs(difference) &gt; start * 0.001:
              if difference &lt; 0:
                  if abs(difference) &lt; guess:
                      print "payment is %s" % guess
                      return evolution(start, guess, interest, months)
                  else:
                      mod = int(abs(difference) / start * guess)
                      if mod == 0:
                          mod = 1
                      guess -= mod
              else:
                  mod = int(difference / start * guess)
                  if mod == 0:
                      mod = 1
                  guess += mod
          else:
              print "payment is %s" % guess
              return evolution(start, guess, interest, months)
  </code></pre>
  
  <p>evolution is just a function that displays how the loan would look like payment for payment and interest for interest, summing up total amount of interest paid etc.</p>
  
  <p>An example would be if I wanted to find out the monthly payments for a loan starting with $100k and ending at $50k with an interest of 8% and a duration of 70 months, calling</p>
  
  <pre><code>&gt;&gt;&gt; find_payment(100000, 50000, 70, 0.08)
  payment is 1363
  </code></pre>
  
  <p>In the above case I would end up owing 49935, and I went through the loop 5 times. The amount of times needed to go through the loop depends on how close I want to get to the amount and it varies a bit.</p>