Search code examples
javamathfinanceformula

Is there a Java API or built-in function for solving annuity problems?


I was asked by my boss to create a module for calculating reverse compound.

The question is: if I want to achieve $1.000.000,00 in 24 months with interest rate 18%/year (or 1.5%/month). how much money do I have to save every month?

I searched on the internet, but found nothing except people referring to the Excel formula. Do you know what the mathematical formula is for this case?

I am using Java for this module. Is there any Java library or API?


Solution

  • Let us say that you are investing D dollars at the beginning of each month for M months earning an interest rate of r compounded monthly. We will set i = r / 12. At the end of M months you will have

    D * (1 + i)^M + D * (1 + i)^(M - 1) + D * (1 + i)^(M - 2) + ...
        D * (1 + i)
    

    in your account. This is because the D dollars in the first month are invested for M months, the D dollars in the second month are invested for M-1 months, and so on. This is a geometric progression and simplifies to

    D * (1 + i) * ((1 + i)^M - 1) / i.
    

    Therefore, if you want X in your account at the end of M months you solve

    X = D * (1 + i) * ((1 + i)^M - 1) / i
    

    for D to obtain

    D = X * i / ((1 + i) * ((1 + i)^M - 1)).
    

    You don't really need an API here to solve this as you can see the solution is quite simple. The concept that you might want to read about here is that of annuities.