# Compound interest with additions [closed]

You have been given the charge to calculate the current balance as of the day that you perform the calculation for 330,000 individuals who worked for an average of 30 years spanning 300 years where the initial principal was 1 with an addition of 1 per day and interest rate set at 3%.

You initially tried to use online compound interest with additions calculators to compute the sum. But all 10 calculators you tried had different results due to rounding errors. You decide to compose your own code which will assist in completing your charge.

## Input

Your task is write a function f which takes as input

• p Principal. A positive integer or decimal reflecting initial deposit.
• r Rate. A positive decimal reflecting the annual interest rate.
• t Time. An positive integer reflecting years to compute the compounding interest with additions.
• n Periods. A positive integer reflecting the number of periods per year to compound interest.
• a Addition. A positive integer or decimal which will be added to the current principal at the end of each period. Any number of additions can be deposited during t; for example, 10 additions of 20 can be deposited in a single day, or no deposits can be made during a period. For this question a will be a constant value, to avoid confusing users who expect output to be a single, consistent value.

## Output

The output can be an array of $$\n\$$ arrays of numbers, where each element is [year, day, days, principal] as follows:

• year The year as a positive integer within t years.

• doy (day of year): The day number within a year; $$\0 \le\$$ day $$\\le 360\$$ e.g. days = 460 implies year = 2 and doy = 100$. • days The number of days since the initial deposit p was made. • balance The current balance. Other acceptable output formats: • The above, but with elements of each array in a different (consistent) order; • List of comma or space-delimited strings; • The above, joined by newlines or tabs; • JSON string representation of an array of objects; • Spreadsheet; • Any other formats permitted by default. ## Important • Interest is compounded at the end of each period, which produces a new principal beginning at the next period. The addition, if any, is added to the new principal after interest is applied at the end of the period. • "Leap day" and "leap years" are irrelevant to the question. The requirement for this question is to use the constant banker's year consisting of 360 days. ## Example Input p=1,r=0.03,t=1,n=12,a=10  Output (truncated, see stacksnippet). You need not output the labels "year", "day", etc. [ { "year": 1, "doy": 0, "days": 0, "balance": 1, "rate": 0.03, "addition": 0 }, { "year": 1, "doy": 30, "days": 30, "balance": 11.0025, "rate": 0.03, "addition": 10 },... { "year": 1, "doy": 180, "days": 180, "balance": 61.391346409181246, "rate": 0.03, "addition": 10 },... { "year": 1, "doy": 360, "days": 360, "balance": 122.69424361094278, "rate": 0.03, "addition": 10 } ]  [ { "year": 1, "DOY": 0, "days": 0, "balance": 1, "rate": 0.03, "addition": 0 }, { "year": 1, "DOY": 30, "days": 30, "balance": 11.0025, "rate": 0.03, "addition": 10 }, { "year": 1, "DOY": 60, "days": 60, "balance": 21.03000625, "rate": 0.03, "addition": 10 }, { "year": 1, "DOY": 90, "days": 90, "balance": 31.082581265625, "rate": 0.03, "addition": 10 }, { "year": 1, "DOY": 120, "days": 120, "balance": 41.160287718789064, "rate": 0.03, "addition": 10 }, { "year": 1, "DOY": 150, "days": 150, "balance": 51.26318843808603, "rate": 0.03, "addition": 10 }, { "year": 1, "DOY": 180, "days": 180, "balance": 61.391346409181246, "rate": 0.03, "addition": 10 }, { "year": 1, "DOY": 210, "days": 210, "balance": 71.5448247752042, "rate": 0.03, "addition": 10 }, { "year": 1, "DOY": 240, "days": 240, "balance": 81.72368683714221, "rate": 0.03, "addition": 10 }, { "year": 1, "DOY": 270, "days": 270, "balance": 91.92799605423507, "rate": 0.03, "addition": 10 }, { "year": 1, "DOY": 300, "days": 300, "balance": 102.15781604437065, "rate": 0.03, "addition": 10 }, { "year": 1, "DOY": 330, "days": 330, "balance": 112.41321058448158, "rate": 0.03, "addition": 10 }, { "year": 1, "DOY": 360, "days": 360, "balance": 122.69424361094278, "rate": 0.03, "addition": 10 } ] ## Formula You can use the formula at Saving and Investing If D represents the amount of a regular deposit, r the annual interest rate expressed as a decimal, m the number of equal compounding period (in a year), and t time in years, then the future value, F, of the account, is: or any comparable formula which determines a future value that you decide or is available at the language that you use, for example the formulas located at ## Winning criteria Shortest code in bytes. ## closed as unclear what you're asking by Peter Taylor, O.O.Balance, Luis felipe De jesus Munoz, Post Left Garf Hunter, Dennis♦Nov 20 at 13:34 Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question. • @guest271314 By oddly restrictive, I mean that the output format seems to be very restrictive such that it might disqualify some of the most popular languages used. Furthermore, the information in the output could effectively be conveyed at each step rather than all at the end, and communicate the same information. I don't see any real reason for such an inflexible output format. – Gigaflop Nov 19 at 20:48 • @guest271314, please leave your attitude at the door, take some time to learn what this site is about, listen to the advice being offered to you in order to help you improve your challenges and, above all, start using the Sandbox. All but one of your challenges have been poorly received and your continued response to those trying to help you out has been to resort to throwing insults around. – Shaggy Nov 19 at 21:22 • @guest271314 I don't object to your being direct nor do I downvote questions for that reason. Personally I think there is room on this site for well-written challenges targeted at certain classes of languages, even as the majority of questions on this site move away from it. However, there are strong consensus on meta that challenges should be written so that most languages can participate, that jumping through hoops to fit an output format removes fun, and that creators of a challenge should edit challenges to fit community guidelines. If you disagree, make a persuasive argument on meta. – lirtosiast Nov 19 at 22:01 • "Accuracy must be to at least 14 decimal places". This is impossible using IEEE 754 double width numbers unless you add some tight constraints which ensure that the final value doesn't exceed about 10. If your intention is that all answers must use a type like Java's BigDecimal then you should make that explicit. – Peter Taylor Nov 20 at 8:00 • The section on output doesn't asks for an array of objects but doesn't say how many. Is it one for each period? What if the periods aren't an integral number of days (as very few are)? How are leap years handled? If the output is supposed to be for one year, why does it not end on year 2, day 0? – Peter Taylor Nov 20 at 8:13 ## 1 Answer ## Haskell, 115 112 bytes d=360 (r#t)n a=zipWith(\y x->(max(ceiling$y/d)1,until(<=d)(-d+)\$y*d/n,y*d/n,x))[0..t*n].iterate(\x->x+(x*r)/n+a)


Function # takes the parameters in order r, t, n, a, p and returns a list of 4-tuples (year, day, days, principal).

Try it online!

• Floating point numbers can be returned for day and days to save a few bytes. Does the program update days when t is 2? – guest271314 Nov 20 at 1:13
• @guest271314: See output for t=2, n=360: Try it online! – nimi Nov 20 at 1:15
• Interesting. Got 3655.4199681711298 for day 360 here using JavaScript. The results at linked tio are 3655.419968171068 – guest271314 Nov 20 at 1:23
• @guest271314: maybe my code is one of the many online compound interest services mentioned in the introduction of your challenge that accumulates rounding errors. – nimi Nov 20 at 1:29