Skip to content

Instantly share code, notes, and snippets.

@strboul
Last active March 16, 2018 21:46
Show Gist options
  • Save strboul/aee10205e5222030947c25c4b64b1beb to your computer and use it in GitHub Desktop.
Save strboul/aee10205e5222030947c25c4b64b1beb to your computer and use it in GitHub Desktop.
compound_interest.R
# How much interest etc. would you pay for a mortgage?
compound <- function(rate, money, n) {
prin <- money * (1+rate/100)^(0:(n-1))
int <- prin * (rate/100)
totalInt <- sum(int)
totalMoney <- money + totalInt
rentpermonth <- totalMoney / (n * 12)
textTable <- c(
cat(rep("=", 30), "\n ", sep=""),
"Rate: %g \n",
"Money: %g \n",
"Year: %g \n",
"Total Interest: %g \n",
"Total Money: %g \n",
"Rent Per Month: %g \n"
)
cat(
sprintf(paste(textTable, collapse = " "),
rate, money, n, totalInt, totalMoney, rentpermonth)
)
}
# Run an example:
compound(rate = 0.99, money = 350000, n = 30)
compound(rate = 0.21, money = 250000, n = 30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment