0

Question:

I am trying to find the mathematical formula used in conjunction with this C++ code below. I am trying calculate by hand the solution be hand.I have found formulas involving logarithms but nothing below. and the solution they provide do not seem to match up with my answers. Any insight appreciated.


The code below is the solution to a ACM regional programming contest problem:

Using credit cards for your purchases is convenient, but they have high interest rates if you do not pay your balance in full each month. The interest rate is commonly quoted in terms of “annual percentage rate” (APR) which is then applied to the outstanding balance each month. The APR can be converted to a monthly interest rate R. At the end of each month, the monthly interest rate is applied to the outstanding balance and the interest is added to the total balance. Any payment made will be applied to the balance in the following month. The monthly interest is rounded to the nearest cent (rounding up 0.5 cent and above) in the calculations. You have unfortunately accumulated an outstanding balance B at the end of the month and you can only afford to pay up to some amount M every month. If you do not make any more purchases with the credit card, what is the minimum number of payments needed to completely eliminate the outstanding balance? It is possible that you cannot pay off the balance in 100 years (1200 payments). Input The input consists of multiple test cases. The first line of input is a single integer, not more than 1000,indicating the number of test cases to follow. Each of the following lines specify the input for one case. Each line contains three positive real numbers separated by single spaces: R, B, and M. The real numbers have two digits after the decimal point, satisfying R 50.00 and B,M 50000.00. R is the monthly interest rate and is specified as a percentage. Output For each case, display on a line the minimum number of payments needed to eliminate the outstanding balance. If this cannot be done in at most 1200 payments, print instead impossible.

Code:

#include <iostream>

using namespace std;

const double eps = 1e-8;
const double max_b = 50000;
const int max_payments = 1200;

int main()
{
    int tn;
    for (cin >> tn; tn--;)
    {
        double r, b, m;
        cin >> r >> b >> m;

        int minnum = 0;
        double pb = max_b + 1;
        while (b > 0 && minnum++ <= max_payments && b < pb)
        {
            pb = b;
            b *= (1 + r / 100.0);
            b = (int)(b * 100 + 0.5 + eps) / 100.0;
            b -= m;
        }
        if (minnum > max_payments || b >= pb)
            cout << "impossible" << endl;
        else
            cout << minnum << endl;
    }

    return 0;
}

============

Sample Input

11
2.00 100.00 105.00
2.00 100.00 102.00
2.00 100.00 100.00
2.00 100.00 4.00
2.00 100.00 3.00
2.00 100.00 1.00
2.00 100.00 2.00
9.56 5462.50 522.22
12.50 29876.44 33610.99
5.50 1.00 1.05
14.78 40181.09 46119.86

Sample Output

1
1
2
36
56
impossible
impossible
impossible
2
2
1
Jongware
  • 22,200
  • 8
  • 54
  • 100
user1560265
  • 57
  • 1
  • 2
  • 8
  • @Shomz: He's trying to figure out the Math, and then apply it. So far, he's showing the application of his attempt, but in fact that's not adequately necessary as this is really a Math question. – jrd1 Mar 24 '14 at 00:37
  • 1
    @jrd1 Right, definitely doesn't seem like a programming issue. – Shomz Mar 24 '14 at 00:40

2 Answers2

0

The math behind this deals with compound interest as:

... At the end of each month, the monthly interest rate is applied to the outstanding balance and the interest is added to the total balance. Any payment made will be applied to the balance in the following month. ...

As such, there is a vast body of work dedicated to understanding this. Wikipedia has a great one:

http://en.wikipedia.org/wiki/Compound_interest

Hint: Pay particular attention to the section on monthly payment.

jrd1
  • 10,358
  • 4
  • 34
  • 51
0

I think a lot of the difficulty is in intentionally obfuscated variable names. Here's the code with readable names:

#include <iostream>

using namespace std;

const double epsilon = 1e-8;
const double max_balance = 50000;
const int max_payments = 1200;

int main()
{
    int totalNumberOfCases;
    for (cin >> totalNumberOfCases; totalNumberOfCases--;)
    {
        double rate, balance, maxMonthlyPayment;
        cin >> rate >> balance >> maxMonthlyPayment;

        int minMonthsToPayoff = 0;
        double previousBalance = max_balance + 1;
        while (balance > 0 && minMonthsToPayoff++ <= max_payments && balance < previousBalance)
        {
            previousBalance = balance;
            balance *= (1 + rate / 100.0);
            balance = (int)(balance * 100 + 0.5 + epsilon) / 100.0;
            balance -= maxMonthlyPayment;
        }
        if (minMonthsToPayoff > max_payments || balance >= previousBalance)
            cout << "impossible" << endl;
        else
            cout << minMonthsToPayoff << endl;
    }

    return 0;
}
Avi Tevet
  • 778
  • 1
  • 7
  • 13