# Algorithm for multiple-debt payoff to minimize time in debt

I'm not sure this is the right place to ask, but here goes anyway.

### Background, if you're interested

I've written some pretty fast C code that can accept an ordered list of loans (with current balances, terms, and rates) and will calculate minimum payments for me. Not only this, but it will also calculate an amortization table that

1. can accept an extra monthly payment to principle and
2. will snowball the minimum payments of paid-off loans into that extra monthly payment.

I'm now challenged with the task of ordering the loans correctly. Going through every possible combination would take O(nn) time (that is, if I have 20 loans, there are 2020 permutations that I have to check if I wanted to check them all). If you want a chuckle, it would take about six times the age of the universe to do it this way.

Clearly, this is not a viable option. I attempted to use the debt-snowball / debt-avalanche methods to determine ordering, but these also appear to not always give the best orderings.

At the moment, I've no better approach than running many thousands (recently ≈25000) of random trials and seeing which loan-orders give the best results (i.e., take the least amount of time total to pay off).

### Is there a good way to determine which loan would be most advantageous to pay off first in terms of months spent paying them off?

For each loan, I've only got the current balance, the interest rate, and the term of the loan (and thus the minimum payment). For the entire loan collection, I have a single extra payment to principle that I'll apply to one loan at a time (letting it snowball). Can I derive an optimal payoff-order of the loans from this information? Is there some other information I could derive and then use? (Is paying off one loan at a time even the best approach?)

• Well, no. It's 20! That's how many ways one can order 20 different items. About 2.4 x 10E18. But even then, this is all nonsense as it's not rocket science to pay highest rate first. That's it. – JTP - Apologise to Monica Sep 18 '17 at 2:48
• If you want to improve your algorithm you should post the question on a CS-related site, I guess – Philipp Sep 22 '17 at 6:39
• @Philipp I disagree; a financial algorithm takes financial expertise to formulate. I wouldn't go to a career mathematician for a recipe. – Sean Allred Sep 22 '17 at 12:22
• I have written something about it with calculations if you want some numbers github.com/SiloCityLabs/loanPay – ldrrp Aug 30 '19 at 16:59