problemAIAlgebra 2
Compound interest
$1000 at 5% a year becomes $1628.89 in ten years if the interest is added once a year, and $1647.01 if it is added monthly. Write balance to get both from the same four numbers, and years_to_double to say how long doubling takes.
Open in the workbenchDownload .vivOutputs below are from the site build’s run; Run all runs it again in your browser.
Look inside this program
- Atlas
The program’s map: every function and which calls which.
There’s no recorded run of this program yet.
Interest added times a year is each time, applied times over years. Getting that substitution right is most of the exercise, and the two numbers above are far enough apart that a test fails if you use and where and belong.
Monthly beats annual by dollars here. Weekly beats monthly by less, daily by less again, and no matter how often the interest is added the balance never passes . Once the tests pass, try a few values of to see it approach that limit.
years_to_double is the question the doubling-time post answers: is in the
exponent, and taking the logarithm of both sides brings it down.
Your task
Write these 2 functions:
balance(p, r, n, t)- The balance after t years: A = P\left(1 + \frac{r}{n}\right)^{nt}.
years_to_double(r)- Years for money at annual rate r, compounded once a year, to double.
Make these tests pass 3 of 4 fail
- $1000 at 5% for 10 years, compounded once a year, is $1628.89 assert_near: |1000 - 1628.89| = 628.89 > 0.01 (fails)
- compounded monthly it is $1647.01 assert_near: |1000 - 1647.01| = 647.01 > 0.01 (fails)
- no time, no interest: the balance is the principal (passes)
- at 5% a year the money doubles in 14.21 years, and the balance then is 2P assert_near: |0 - 14.2067| = 14.2067 > 0.001 (and 1 more) (fails)
Printed